RESTX business errors management
Restx provides a powerful way to handle errors in consistent way throughout your application.
Defining Errors
RESTX introduce a kind of DSL to define your business errors.
You define your application errors using annotated enums, usually grouped in a class called Rules, which we recommend to define as an inner class of the domain class to which the rules apply.
The values of the enum are the parameters that sould be provided when raising such an error.
The use of enum has been chosen for definition readability and code completion when raising the error.
It should be considered as a DSL, not a real enum
Here is an example:
Here we have defined 2 business errors:
- CompanyRef business error indicates a user must have a company, and takes one parameter called KEY which is the user key.
- ValidCompanyRef business error indicates that the company refecrenced by a user must exist
The codes provided in the annotations must be unique, and is used for easy referencing the business error.
Injection
You need to inject a RestxErrors component into your component constructor (see for injections if necessary) :
Raising errors
RESTX doesn’t provide facility for checking the rule, so you have to do the check on your own. But then if your check fails you can raise the error like this:
The API is fluent, and type safe, with pretty good code completion to provide the parameters.
Consuming errors
On the client side, errors are provided as:
- a 400 http status code (this can be configured using the
status
parameter on the@ErrorCode
annotation) - a json object detailing the error
the json object looks like this:
Documenting errors
Error definitions are automatically documented in the admin console on /@/ui/errors/
:
As you can see the console provides the list of business errors, with:
- the error code
- the associated http status
- the description
- the associated parameters, with their names and description