Open beauby opened 6 years ago
Thanks for this comment. Solved it as follows:
class ApiController < ApplicationController
include ExceptionHandler
end
module ExceptionHandler
# provides the more graceful `included` method
extend ActiveSupport::Concern
included do
rescue_from Exception do |exception|
case exception
when ActiveRecord::RecordNotFound
render jsonapi_errors: {message: exception.message}, status: :not_found
when ActiveRecord::RecordInvalid
render jsonapi_errors: {message: exception.message}, status: :unprocessable_entity
# else
# figure out how to render_500
end
end
end
end
Instead of having
render jsonapi: foo
andrender jsonapi_errors: errors
, simply haverender jsonapi: foo
andrender jsonapi: errors, status: 400
.