module JsonapiErrorsHandler

Allows to handle ruby errors and return the serialized JSON:API output

Constants

PREDEFINED_HASH
VERSION

Public Class Methods

configure(&block) click to toggle source
# File lib/jsonapi_errors_handler.rb, line 55
def self.configure(&block)
  Configuration.instance.configure(&block)
end
included(base) click to toggle source
# File lib/jsonapi_errors_handler.rb, line 23
def self.included(base)
  base.class_eval do
    ErrorMapper.map_errors!(PREDEFINED_HASH)
  end
end

Public Instance Methods

handle_error(error) click to toggle source
# File lib/jsonapi_errors_handler.rb, line 29
def handle_error(error)
  log_error(error) if respond_to?(:log_error)
  # Handle every error which inherits from
  # JsonapiErrorsHandler::Errors::StandardError
  #
  if JsonapiErrorsHandler::ErrorMapper.mapped_error?(error.class.superclass.to_s)
    return render_error(error)
  end

  mapped = ErrorMapper.mapped_error(error)
  mapped ? render_error(mapped) : handle_unexpected_error(error)
end
handle_unexpected_error(error) click to toggle source
# File lib/jsonapi_errors_handler.rb, line 42
def handle_unexpected_error(error)
  config = JsonapiErrorsHandler::Configuration.instance
  raise error unless config.handle_unexpected?

  notify_handle_unexpected_error(error) if respond_to?(:notify_handle_unexpected_error)

  render_error(::JsonapiErrorsHandler::Errors::StandardError.new)
end
render_error(error) click to toggle source
# File lib/jsonapi_errors_handler.rb, line 51
def render_error(error)
  render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
end