Welcome to FindNerd, today we are going to discuss exceptions in CakePHP.
We can use exceptions for a variety of uses in our application when implementing any web application in CakePHP. CakePHP uses the exceptions internally to show the logical cake PHP errors and all exceptions occur in CakePHP when CakeException is raised.
Note:- we can use error handler to catch uncaught exceptions from controllers and other parts of our application
CakePHP has numbers of exception classes that we can use for HTTP errors.
For configuring exceptions in CakePHP, we can use the below code.
'handler' => 'ErrorHandler::handleException',//The callback to handle exceptions. You can set this to any callback type,
'renderer' => 'ExceptionRenderer',// The class responsible for rendering uncaught exceptions.
'log' => true // boolean - When true, exceptions + their stack traces will be logged to CakeLog.
If we change the handler then this change will allow us to take full control over the exception handling process. If we change the renderer, then it will allow us to change the output.
Detail of exception classes
There are a number of exception classes in CakePHP. Each exception replaces a cakeError() error messages.
There are several built-in exceptions inside CakePHP
1-Exception BadRequestException :- This exception is used for doing 400 Bad Request error.
2-Exception UnauthorizedException:- This exception is used for doing a 401 Unauthorized error.
3-Exception ForbiddenException :- This exception is used for doing a 403 Forbidden error.
4-Exception NotFoundException:- This exception is used for doing a 404 Not found error.
5-Exception MethodNotAllowedException:- This exception is used for doing a 405 Method Not Allowed error.
6-Exception InternalErrorException :- This exception is used for doing a 500 Internal Server Error.