Repository URL to install this package:
|
Version:
0.1.0 ▾
|
# frozen_string_literal: true
module Kiwi
module Errors
# Error Renderer
module Renderer
def render_error(status, code, title, detail, exception = nil)
error = construct_error status, code, title, detail
if Hanami.env == 'production'
message = {
errors: [error]
}
halt status, message.to_json
else
non_production_render error, exception, status
end
end
def construct_error(status, code, title, detail)
{
id: SecureRandom.uuid,
code: code || 'unknown',
status: status.to_s || '500',
title: title || 'Unknown',
detail: detail || 'An unknown error has occurred and has been logged.'
}
end
def non_production_render(error, exception, status)
message = non_production_message(error, exception)
halt status, message
end
def non_production_message(error, exception) # rubocop:disable Metrics/LineLength, Metrics/MethodLength
message = if exception
{
errors: [error],
exception: exception.class.name,
message: exception.message,
trace: exception.backtrace[0, 10]
}
else
{ errors: [error] }
end
message
end
end
end
end