Handling Error in Cakephp 2
Cakephp is a good framework, it include many aspect of building a website fast. Handling error in cakephp 2 is easy, by default you can change template for all type of error inside folder app/Views/Errors (in prior version app/views/errors). You can create each template for each error, such as
Http Error
error400.ctp
error404.ctp
error500.ctp, etc
Missing File
missing_action.ctp
missing_component_class.ctp
missing_component_file.ctp
missing_connection.ctp
missing_controller.ctp
missing_helper_class.ctp
missing_helper_file.ctp
missing_layout.ctp
missing_model.ctp
missing_scaffolddb.ctp
missing_table.ctp
missing_view.ctp
Private & scaffold related errors
private_action.ctp
scaffold_error.ctp
# missing files #
missing_action.ctp
missing_component_class.ctp
missing_component_file.ctp
missing_connection.ctp
missing_controller.ctp
missing_helper_class.ctp
missing_helper_file.ctp
missing_layout.ctp
missing_model.ctp
missing_scaffolddb.ctp
missing_table.ctp
missing_view.ctp
# private & scaffold related errors #
private_action.ctp
scaffold_error.ctp
How to Error handling in cakephp 2 for PHP errors
In cakephp 2, error handling and exception handling are more better, the method cakeError now replaced by many built in Exception. So if you want to handle missing view etc, or any build in Exception, you need to check Exception handler setting. For PHP error that occured on view, we can handle it separately by using Error configuration.
To configure the error handling on php, open app/Config/core.php, find this line
Configure::write('Error', array(
'handler' => 'ErrorHandler::handleError',
'level' => E_ALL & ~E_DEPRECATED,
'trace' => true
));
handler is a callback function that handling the error on cakephp
level is built-in php error constants
trace true if you want to include trace message when displaying error message
If you want to handle the error in cakephp using your own class, create app/Lib/AppError.php
class AppError {
public static function handleError($code, $description, $file = null, $line = null, $context = null) {
echo 'There has been an error!';
}
}
and then add this line to app/Config/bootstrap.php to load the class
App::uses('AppError', 'Lib');
after that set app/Config/core.php
Configure::write('Error', array(
'handler' => 'AppError::handleError',
'level' => E_ALL & ~E_DEPRECATED,
'trace' => true
));
You can set default error layout by create new error layout template in app/View/Layouts/error.ctp, otherwise it will use default layout to display error message.
Handling Exception in cakephp 2
First you need to know several built in exception in cakephp 2, such as BadRequestException, ForbiddenException, NotFoundException, etc. You can find complete list on this link.
take a look at app/Config/core.php
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'ExceptionRenderer',
'log' => true
));
You can create custom exception handler in app/Lib/AppExceptionHandler.php
class AppExceptionHandler {
public static function handle($error) {
echo 'Oh noes! ' . $error->getMessage();
// ...
}
// ...
}
and then add this line to app/Config/bootstrap.php
App::uses('AppExceptionHandler', 'Lib');
finally set your custom exception handler in app/Config/core.php
Configure::write('Exception.handler', 'AppExceptionHandler::handle');