Implementing API Exceptions
The better solution than using to signal errors for invalid APIusage is to implement your own exception type and install an error handlerfor it that produces the errors in the format the user is expecting.
The basic idea is to introduce a new exception that can take a properhuman readable message, a status code for the error and some optionalpayload to give more context for the error.
A view can now raise that exception with an error message. Additionallysome extra payload can be provided as a dictionary through the _payload_parameter.
At that point views can raise that error, but it would immediately resultin an internal server error. The reason for this is that there is nohandler registered for this error class. That however is easy to add:
Here is how a view can use that functionality: