Exceptions
#
Definition & PrinciplesRead Porto SAP Documentation (#Exceptions).
#
Rules- All Exceptions MUST extend
App\Ship\Parents\Exceptions\Exception
. - Shared (general) Exceptions between all Containers SHOULD be created in the Exceptions Ship folder (
app/Ship/Exceptions/*
). - Every Exception SHOULD have two properties
code
andmessage
. You can override those values while throwing the error.
#
Folder Structure - App - Containers - {section-name} - {container-name} - Exceptions - AccountFailedException.php - ...
- Ship - Exceptions - CreateResourceFailedException.php - NotFoundException.php - ...
#
Code Samples#
Demo Exceptionclass DemoException extends Exception{ public $code = Response::HTTP_CONFLICT; public $message = 'This is a demo exception.';}
#
Usage from anywherethrow new AccountFailedException();
#
Usage with errorsthrow (new AccountFailedException())->withErrors(['email' => 'Email already in use']);throw (new AccountFailedException())->withErrors(['email' => ['Email already in use', 'Another message']]);
#
Usage with errors and localizationFor localization, you can use the Localization Container
// translation strings are automatically translated if the translations are found.throw (new AccountFailedException())->withErrors(['email' => 'appSection@user::exceptions.email-taken']);
Response:
{ "message": "The exception error message.", "errors": { "email": [ "The email has already been taken." ] }}
#
Usage with Log for Debuggingthrow (new AccountFailedException())->debug($e); // debug() accepts string or \Exception instance
#
Usage and overriding the defaultthrow new AccountFailedException('I am the message to be displayed to the user');