Re: [PHP] Re: trapping fatal errors...?

2006-06-13 Thread Eric Butera

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.


I read what you said and understand where you are coming from, but if
you call a function and it doesn't exist, your script is going to die.
The only thing you can do is just tail your error log and write unit
tests to make sure your stuff is working right.  If you are on a local
machine just keep an eye on your php error log on each request and you
will see the full error message if log errors is turned on.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: trapping fatal errors...?

2006-06-13 Thread Jochem Maas
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('bNotice/b:  ') ||
 response.indexOf('bWarning/b:  ') ||
 response.indexOf('bFatal 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