As you can see from the above code, these exceptions can only specify the message and status_code parameters, so we can customize the exception and then add the The following code is modified according to this idea:

The following two methods for custom exception handling are described:

Using response. Json.

This method is simple, since the sanic exception handling is to use the response, text ( ) method back to the response. The sanic response provides the respo & e object for the json. You can define a error handling method by using response.json: