Error Handling
We want to follow the common best-practice of raising a specific error class:
raise Errors::NotAuthorized
The problem is, this error would cause our server to return a 500
status code, without much helpful detail. Instead, we want to
customize our responses based on the error thrown.
Enter jsonapi_errorable, which provides a simple DSL to do just that:
# app/controllers/application_controller.rb
register_exception Errors::NotAuthorized, status: 403
Here we’ve customized the error response to send the HTTP status code 403
(forbidden) whenever this particular exception class is raised.
Maybe our error class already has a message we want to display to the user:
register_exception Errors::NotAuthorized,
status: 403,
title: "Not Authorized",
message: true,
For full documentation on everything you can do here, head over to the jsonapi_errorable documentation.