Created
December 18, 2011 17:17
-
-
Save mehlah/1493959 to your computer and use it in GitHub Desktop.
Lithium Error handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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