Repository URL to install this package:
|
Version:
0.1.7 ▾
|
# frozen_string_literal: true
module ElixirPay
module Errors
# Error Renderer
module Handler
def api_errors(exception)
if exception.is_a? ElixirPay::Errors::ApiError
api_error(exception)
else
normal_error(exception)
end
end
def api_error(exception)
status = exception.status || 500
code = exception.code || 'E99999'
if status == 422
record_invalid(exception)
else
render_error status,
code,
exception.title,
exception.message
end
end
def normal_error(exception)
status = 500
code = exception
render_error(
status, code, exception.class.name,
exception.message
)
end
def record_invalid(exception)
formatted_errors = []
used_fields = []
record = JSON.parse(exception.message)
record.each do |error, value|
next if used_fields.include? error.to_s
formatted_error = {
field: error.to_s,
detail: value
}
formatted_errors.push formatted_error
used_fields.push error.to_s
end
formatted_errors = formatted_errors.sort_by { |k| k[:field] }
render_error(422,
exception.code,
exception.title,
formatted_errors)
end
end
end
end