Skip to content

Commit

Permalink
error_response and error! have same signature. Fixes ruby-grape#889.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Juneja committed Jan 23, 2015
1 parent 567e779 commit d912511
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'grape/middleware/base'

module Grape
module Middleware
class Error < Base
Expand All @@ -21,7 +20,6 @@ def default_options

def call!(env)
@env = env

begin
error_response(catch(:error) do
return @app.call(@env)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d912511

Please sign in to comment.