Exceptions are classes the handles errors, and helps developers debug their code in a more efficient way.
- Exceptions can be thrown from anywhere in the application.
- Exceptions SHOULD be created inside the Containers. However, general Exceptions CAN be created in the
- All Exceptions MUST extend
- Shared (general) Exceptions between all Containers SHOULD be created in the Exceptions Ship folder (
- Every Exception SHOULD have two properties
message, both properties will be displayed when an error occurs. You can override those values while throwing the error.
Exception with CustomData:
Exception usage from anywhere:
Usage with Log for Debugging:
Usage and overriding the default
Usage and overwriting pre-set CustomData
Apiato provides a convenient way to manage all
application error codes in one central place. Therefore, Apiato provides, amongst others, the
\App\Ship\Exceptions\Codes\ApplicationErrorCodesTable class, which already holds various information for multiple errors.
Thereby, one error look like this:
Note that the
code is used to be sent back to the client. The
description, however, can be used to automatically generate a documentation regarding all defined error codes and their meaning. Please note that this feature is currently not implemented but will be added later on.
In order to link an
error code to an
Exception, you simply need override the
useErrorCode() method of the
Consider the following example:
Please note that already defined
$code values may be overwritten by the
useErrorCode() method! Furthermore, this feature is completely optional - you may still use the known
public $code = 4711; approach to manually set an error code.
Of course, Apiato allows you to define your own
CustomErrorCodesTable. In fact, there already exists such a file where you can define your own error codes. Please note that the
ApplicationErrorCodesTable may be adapted by Apiato - the others will not.
If you like to split the errors in various files, you can easily create a
UserErrorCodesTable in respective namespace and define the errors accordingly. However, you need to manually "register" this code table. This can be achieved in the
Now you can easily use your
UserErrorCodesTable::USER_NOT_VERIFIED error in your