lib/easyship/middleware/error_handler_middleware.rb in easyship-0.1.1 vs lib/easyship/middleware/error_handler_middleware.rb in easyship-0.1.2

- old
+ new

@@ -23,14 +23,40 @@ def raise_error(class_error, body) raise class_error.new(message: message(body), body_error: body_error(body)) end def body_error(body) - body.is_a?(Hash) ? body[:error] : {} + return {} unless body.is_a?(Hash) + + if body.key?(:error) && body[:error].is_a?(Hash) + format_body_error(body) + elsif body.key?(:errors) && body[:errors].is_a?(Array) + format_body_errors_array(body) + elsif body.key?(:errors) + format_body_errors(body) + else + format_by_default(body) + end end def message(body) - body.is_a?(Hash) ? body[:error][:message] : body + body_error(body)[:message] + end + + def format_body_error(body) + body[:error] + end + + def format_body_errors_array(body) + { details: body[:errors], message: body[:errors].map { |error| error[:message] }.join(', ') } + end + + def format_body_errors(body) + { details: body[:errors], message: body[:errors] } + end + + def format_by_default(body) + { details: body, message: 'Something went wrong.' } end def json?(body) !body.nil? && body.is_a?(String) end