Skip to main content
Version: 10.x

Exceptions

Definition & Principles

Read 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 and message. 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 Exception

class DemoException extends Exception
{
public $code = Response::HTTP_CONFLICT;
public $message = 'This is a demo exception.';
}

Usage from anywhere

throw new AccountFailedException();

Usage with errors

throw (new AccountFailedException())->withErrors(['email' => 'Email already in use']);
throw (new AccountFailedException())->withErrors(['email' => ['Email already in use', 'Another message']]);

Usage with errors and localization

For 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 Debugging

throw (new AccountFailedException())->debug($e); // debug() accepts string or \Exception instance

Usage and overriding the default

throw new AccountFailedException('I am the message to be displayed to the user');