class ApiPack::Errors::HandleError

Attributes

error[RW]

Public Class Methods

new(error) click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 6
def initialize(error)
  @error = error
end

Public Instance Methods

call() click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 10
def call
  handle_error
end

Private Instance Methods

error_message_body(handler:, error:) click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 37
def error_message_body(handler:, error:)
  {
    title: handler[:title],
    status: handler[:status],
    details: error.message
  }
end
handle_error() click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 18
def handle_error
  if ERROR_MAP.keys.include? error.class.name
    handler = ERROR_MAP[error.class.name]
    body = send(handler[:method], handler: handler, error: error)
  else
    body = {
      title: 'Internal Server Error',
      details: error.message,
      status: :internal_server_error
    }
  end

  { body: serialize(body), status: body[:status] }
end
parameter_missing(handler:, error:) click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 45
def parameter_missing(handler:, error:)
  {
    title: handler[:title],
    details: error.message,
    status: handler[:status],
    parameter: error.param
  }
end
record_invalid(handler:, error:) click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 54
def record_invalid(handler:, error:)
  {
    title: handler[:title],
    status: handler[:status],
    details: ApiPack::Errors::ValidationErrorsSerializer.new(error.record).serialize
  }
end
serialize(body) click to toggle source
# File lib/api_pack/errors/handle_error.rb, line 33
def serialize(body)
  ApiPack::Errors::ApiErrorsSerializer.new(body).serializer
end