module JSONAPI::Errors
Helpers to handle some error responses
Most of the exceptions are handled in Rails
by [ActionDispatch] middleware See: api.rubyonrails.org/classes/ActionDispatch/ExceptionWrapper.html
Public Class Methods
included(base)
click to toggle source
Callback will register the error handlers
@return [Module]
# File lib/jsonapi/errors.rb, line 12 def self.included(base) base.class_eval do rescue_from( StandardError, with: :render_jsonapi_internal_server_error ) unless defined?(::Rails) && ::Rails.env.test? rescue_from( ActiveRecord::RecordNotFound, with: :render_jsonapi_not_found ) if defined?(ActiveRecord::RecordNotFound) rescue_from( ActionController::ParameterMissing, with: :render_jsonapi_unprocessable_entity ) if defined?(ActionController::ParameterMissing) end end
Private Instance Methods
render_jsonapi_internal_server_error(exception)
click to toggle source
Generic error handler callback
@param exception [Exception] instance to handle @return [String] JSONAPI
error response
# File lib/jsonapi/errors.rb, line 36 def render_jsonapi_internal_server_error(exception) error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] } render jsonapi_errors: [error], status: :internal_server_error end
render_jsonapi_not_found(exception)
click to toggle source
Not found (404) error handler callback
@param exception [Exception] instance to handle @return [String] JSONAPI
error response
# File lib/jsonapi/errors.rb, line 45 def render_jsonapi_not_found(exception) error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] } render jsonapi_errors: [error], status: :not_found end
render_jsonapi_unprocessable_entity(exception)
click to toggle source
Unprocessable entity (422) error handler callback
@param exception [Exception] instance to handle @return [String] JSONAPI
error response
# File lib/jsonapi/errors.rb, line 54 def render_jsonapi_unprocessable_entity(exception) source = { pointer: '' } if !%w{data attributes relationships}.include?(exception.param.to_s) source[:pointer] = "/data/attributes/#{exception.param}" end error = { status: '422', title: Rack::Utils::HTTP_STATUS_CODES[422], source: source } render jsonapi_errors: [error], status: :unprocessable_entity end