Skip to content

Commit

Permalink
Supporting hash objects in error.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed May 12, 2011
1 parent 8e8fe68 commit 09e7078
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
14 changes: 11 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ And would respond to the following routes:

Serialization takes place automatically. For more detailed usage information, please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).

## Error Handling
## Raising Errors

The default behavior of Grape is to rescue all exceptions and to return a 403 status code with the exception's message in the response body.
You can raise errors explicitly.

It's possible to trap all exceptions by setting `rescue_all_errors true`. This prevents displaying the web server's error page as a result. You may also change the error format to JSON by using `error_format :json` and set the default error status to 200 with `default_error_status 200`. You may also include the complete backtrace of the exception with `rescue_with_backtrace true`.
error!("Access Denied", 401)

You can also return JSON formatted objects explicitly by raising error! and passing a hash instead of a message.

error!({ "error" => "unexpected error", "detail" => "missing widget" }, 500)

## Exception Handling

By default Grape does not catch all unexpected exceptions. This means that the web server will handle the error and render the default error page as a result. It is possible to trap all exceptions by setting `rescue_all_errors true` instead. You may change the error format to JSON by using `error_format :json` and set the default error status to 200 with `default_error_status 200`. You may also include the complete backtrace of the exception with `rescue_with_backtrace true` either as text (for the :txt format) or as a :backtrace field in the json (for the :json format).

class Twitter::API < Grape::API
rescue_all_errors true
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def formatter_for(api_format)
end

def format_json(message, backtrace)
result = { :error => message }
result = message.is_a?(Hash) ? message : { :error => message }
if (options[:backtrace] && backtrace && ! backtrace.empty?)
result = result.merge({ :backtrace => backtrace })
end
result.to_json
end

def format_txt(message, backtrace)
result = message
result = message.is_a?(Hash) ? message.to_json : message
if options[:backtrace] && backtrace && ! backtrace.empty?
result += "\r\n "
result += backtrace.join("\r\n ")
Expand Down
2 changes: 0 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ def app; subject end
end

it 'should accept an object and render it in format' do
pending

subject.get '/hey' do
error!({'dude' => 'rad'}, 403)
end
Expand Down
26 changes: 25 additions & 1 deletion spec/grape/middleware/exception_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
require 'spec_helper'

describe Grape::Middleware::Error do

# raises a text exception
class ExceptionApp
class << self
def call(env)
raise "rain!"
end
end
end


# raises a hash error
class ErrorHashApp
class << self
def error!(message, status=403)
throw :error, :message => { :error => message, :detail => "missing widget" }, :status => status
end
def call(env)
error!("rain!", 401)
end
end
end

# raises an error!
class AccessDeniedApp
class << self
def error!(message, status=403)
Expand Down Expand Up @@ -68,6 +83,15 @@ def app
last_response.body.should == '{"error":"rain!"}'
end

it 'should be possible to return hash errors in json format' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error, :format => :json
run ErrorHashApp
end
get '/'
last_response.body.should == '{"error":"rain!","detail":"missing widget"}'
end

it 'should be possible to specify a custom formatter' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error,
Expand Down

0 comments on commit 09e7078

Please sign in to comment.