Exceptions and Error Handling
ErrorHandler
configuration is done by creating a new error-specific bootstrap file that contains your ErrorHandler
configuration and initialization. To illustrate how this is done, let’s consider an imaginary (but common) scenario. Rather than tossing up error messages and stack traces to your users, it’s better to create some sort of way to handle exceptions and render user-friendly error pages.
Start by creating a new bootstrap file in the application directory called /config/bootstrap/error.php
:
Here’s a more complete example, showing how you’d actually render a template, and include logging:
use lithium\core\ErrorHandler;
use lithium\analysis\Logger;
use lithium\template\View;
Logger::config(['error' => ['adapter' => 'File']]);
$render = function($template, $content) {
'paths' => [
'template' => '{:library}/views/{:controller}/{:template}.{:type}.php',
'layout' => '{:library}/views/layouts/{:layout}.{:type}.php',
]
]);
'controller' => 'errors',
'type' => 'html'
]);
};
ErrorHandler::apply('lithium\action\Dispatcher::run', $conditions, function($exception, $params) {
Logger::write('error', "Page Not Found...");
$render('404', compact('exception', 'params'));
});