Skip to content

Instantly share code, notes, and snippets.

@mehlah
Created December 18, 2011 17:17
Show Gist options
  • Save mehlah/1493959 to your computer and use it in GitHub Desktop.
Save mehlah/1493959 to your computer and use it in GitHub Desktop.
Lithium Error handler
<?php
use lithium\core\ErrorHandler;
use lithium\action\Response;
use lithium\net\http\Media;
use lithium\analysis\Debugger;
use lithium\analysis\Logger;
use lithium\core\Environment;
/**
* set up a basic logging configuration that will write to a file
*/
Logger::config(array(
'error' => array(
'production' => array(
'adapter' => 'File',
'file' => function($data, $config) { return "{$data['priority']}.prod.log"; }
)
)));
ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
$response = new Response(array(
'request' => $params['request'],
'status' => $info['exception']->getCode()
));
if (Environment::is('production')) {
//prepare error data to log
$data = json_encode(array(
'message' => $info['exception']->getMessage(),
'source' => json_encode(array(
'file' => $info['exception']->getFile(),
'line' => $info['exception']->getLine()
)),
'stack_trace' => json_encode(Debugger::trace(array(
'format' => 'array',
'trace' => $info['exception']->getTrace()
))),
'code' => $info['exception']->getCode()
));
//log json encoded errors
Logger::error("{$data}");
//for a 404 show a 404 page and for all other exceptions, show the user a 500 error
$errors_template = ($info['exception']->getCode() == 404) ? '404' : '500';
Media::render($response, compact('info', 'params'), array(
'library' => 'app',
'controller' => '_errors',
'template' => $errors_template,
'layout' => false,
'request' => $params['request']
));
}
else {
Media::render($response, compact('info', 'params'), array(
'controller' => '_errors',
'template' => 'development',
'layout' => 'error',
'request' => $params['request']
));
}
return $response;
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment