diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index a77db42c13..b6d68a22c9 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -1,5 +1,4 @@ require 'grape/middleware/base' - module Grape module Middleware class Error < Base @@ -21,7 +20,6 @@ def default_options def call!(env) @env = env - begin error_response(catch(:error) do return @app.call(@env) @@ -62,12 +60,19 @@ def handle_error(e) error_response(message: e.message, backtrace: e.backtrace) end - def error_response(error = {}) - status = error[:status] || options[:default_status] - message = error[:message] || options[:default_message] - headers = { 'Content-Type' => content_type } + def error_response(error = {}, status = nil, headers = {}) + status ||= (error[:status] || options[:default_status]) + headers = { 'Content-Type' => content_type }.merge(headers) headers.merge!(error[:headers]) if error[:headers].is_a?(Hash) backtrace = error[:backtrace] || [] + error = error.except(:backtrace, :status, :headers) if error.is_a?(Hash) + if error.is_a?(Grape::Exceptions::Base) + message = error[:message] + elsif error.is_a?(Hash) && error.count == 0 + message = options[:default_message] + else + message = error + end rack_response(format_message(message, backtrace), status, headers) end