AdonisJS provides streamlined ways to raise exceptions in your controller or service code, and raise them in a controlled way to the caller.
To create a custom exception -
|
|
This will create -
|
|
Edit the exception to introduce your own errors.
|
|
We are trying to raise a 404 with a specific exception message in the above code block.
Now all you need to do is to use this exception in your code. We will take a controller as an example -
|
|
PS: This is just an example. Normally you would want to do Todo.findOrFail(id)
to automatically raise an exception if the record does not exist. Custom exceptions allow you to have your own validation handlers (for e.g. raise a 403 for heck of it), or custom error messages.