Christopher J. Bottaro wrote: > Adam Zey wrote: > >> Christopher J. Bottaro wrote: >>> Hello, >>> How can I trap a fatal error (like calling a non existant method, >>> requiring >>> a non existant file, etc) and go to a user defined error handler? I >>> tried set_error_handler(), but it seems to skip over the errors I care >>> about. >>> >>> Thanks for the help. >> It is always safer to handle errors before they happen by checking that >> you're in a good state before you try to do something. >> >> For example, nonexistent files can be handled by file_exists(). >> Undefined functions can be checked with function_exists(). >> >> Regards, Adam Zey. > > Well, I know that. > > Sometimes unexpected errors happen. We write hundreds of lines of code a > day. Typos happen, I forget some includes, I type $d->appendCild() instead > of $d->appendChild(). It's all a part of the development process. > > Our application makes extensive use of AJAX and JSON. Sometimes we make an > AJAX request and expect a JSON object in return, but instead a fatal error > happens (DOMDocument::appendChid() does not exist), well now we get a JSON > error because the response headers were messed up by the fatal error. > > That JSON error is useless. We would rather see the real error as PHP would > have reported it on a simple webpage or command line. > > Basically, we just want to trap all errors and reraise them as exceptions so > that our app's default exception handler can report them. for fatal errors this not possible. write test routines to check the output of requests that are usually made by AJAX code... and made use a function like this to cover all your bases: function PHPErrorReturned(response) { // this is a bit crude and could possibly break if we are recieving // [content] HTML as part of the returned data. if ((response.indexOf('<b>Notice</b>: ') || response.indexOf('<b>Warning</b>: ') || response.indexOf('<b>Fatal Error</b>: ')) && (response.indexOf('{') != 0)) { alert("Er was een fout opgetreden op de server\n"+response.stripTags()); return true; } return false; } String.prototype.stripTags = function (validTags) { var newstr = this.toString(); var regExp1 = /<\/?(\w+)(.*?)>/ig; if (validTags && validTags.prototype == Array) { var regExp2 = new RegExp('/^('+validTags.join('|')+')$/i'); // em|strong|u|p } while(mt = regExp1.exec(newstr)) { oldstr = mt[0]; tag = mt[1]; pars = mt[2]; repl = ''; if(regExp2 && tag.match(regExp2)) { repl = oldstr.replace(pars,''); } newstr = newstr.replace(oldstr, repl); } return newstr; } > > Thanks. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php