We are running several symfony2 websites in production and we are using both Airbrake and Sentry to collect exceptions. Real exceptions are working nicely even in production, we can log them with a lot of context, and we can show to user a nice 500 error page.
PHP errors are a different story. I can log them to Sentry using a "on shutdown" handler, but using this I cannot pass along a lot of context. For the user there is also only an empty 503 error. This sucks because we cannot show the user a "next" step in case something went wrong.
Coming from a Java background i quite like "real" exceptions and in development mode there is a class that converts a lot of errors in exceptions, but there is no documented or obvious way of enabling this in production, so I figure maybe I shouldn't before asking around some more ;)
How are you guys handling this, and what kind of performance penalty does it entail?
This is a crosspost from: http://groups.google.com/group/symfony2/browse_thread/thread/6129f57a35d8cb90?hl=en
the PHP way to enable this:
Errors (bootstrap!):
set_error_handler('errorToException');
function errorToException($code, $message, $file = null, $line = 0) {
if (error_reporting() == 0) {
return true;
}
throw new \ErrorException($message, $code, $file, $line);
}
Exceptions (ExceptionHandler class):
set_exception_handler(array($this, 'exception'));
public function exception(\Exception $e) {
$this->logger->log($e);
}
Fatal Errors (ExceptionHandler class):
register_shutdown_function(array($this, 'shutdown'));
public function shutdown() {
$error = error_get_last();
if (isset($error)) {
$this->exception(new \FatalException($error['message'], $error['type'], $error['file'], $error['line']));
}
}
This is the-PHP-way™. All your "Symfony2™" ErrorHandling should stay the same.
In Symfony2, we can introduce Error/Exception listener service (which catches all the errors) and on onKernelException() we can include all of our domain logic to handle error/exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With