Skip to content

Commit

Permalink
Add error! to rescue_from. Fixes ruby-grape#889.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Juneja committed Apr 1, 2015
1 parent e204903 commit 744837c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/grape/error_formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class << self
def call(message, backtrace, options = {}, env = nil)
message = Grape::ErrorFormatter::Base.present(message, env)

result = message.is_a?(Hash) ? message : { error: message }
result = message.is_a?(String) ? { error: message } : message
if (options[:rescue_options] || {})[:backtrace] && backtrace && !backtrace.empty?
result = result.merge(backtrace: backtrace)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def exec_handler(e, &handler)
end
end

def error!(message, status = options[:default_status], headers = {}, backtrace = [])
headers = { 'Content-Type' => content_type }.merge(headers)
rack_response(format_message(message, backtrace), status, headers)
end

def handle_error(e)
error_response(message: e.message, backtrace: e.backtrace)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/exceptions/validation_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def app
it 'can return structured json with separate fields' do
subject.format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
rack_response e.to_json, 400
error!(e, 400)
end
subject.params do
optional :beer
Expand Down

0 comments on commit 744837c

Please sign in to comment.