Skip to content

Commit 09e7078

Browse files
committed
Supporting hash objects in error.
1 parent 8e8fe68 commit 09e7078

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

README.markdown

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ And would respond to the following routes:
6262

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

65-
## Error Handling
65+
## Raising Errors
6666

67-
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.
67+
You can raise errors explicitly.
6868

69-
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`.
69+
error!("Access Denied", 401)
70+
71+
You can also return JSON formatted objects explicitly by raising error! and passing a hash instead of a message.
72+
73+
error!({ "error" => "unexpected error", "detail" => "missing widget" }, 500)
74+
75+
## Exception Handling
76+
77+
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).
7078

7179
class Twitter::API < Grape::API
7280
rescue_all_errors true

lib/grape/middleware/error.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def formatter_for(api_format)
3737
end
3838

3939
def format_json(message, backtrace)
40-
result = { :error => message }
40+
result = message.is_a?(Hash) ? message : { :error => message }
4141
if (options[:backtrace] && backtrace && ! backtrace.empty?)
4242
result = result.merge({ :backtrace => backtrace })
4343
end
4444
result.to_json
4545
end
4646

4747
def format_txt(message, backtrace)
48-
result = message
48+
result = message.is_a?(Hash) ? message.to_json : message
4949
if options[:backtrace] && backtrace && ! backtrace.empty?
5050
result += "\r\n "
5151
result += backtrace.join("\r\n ")

spec/grape/endpoint_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ def app; subject end
7373
end
7474

7575
it 'should accept an object and render it in format' do
76-
pending
77-
7876
subject.get '/hey' do
7977
error!({'dude' => 'rad'}, 403)
8078
end

spec/grape/middleware/exception_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
require 'spec_helper'
22

33
describe Grape::Middleware::Error do
4+
5+
# raises a text exception
46
class ExceptionApp
57
class << self
68
def call(env)
79
raise "rain!"
810
end
911
end
1012
end
11-
13+
14+
# raises a hash error
15+
class ErrorHashApp
16+
class << self
17+
def error!(message, status=403)
18+
throw :error, :message => { :error => message, :detail => "missing widget" }, :status => status
19+
end
20+
def call(env)
21+
error!("rain!", 401)
22+
end
23+
end
24+
end
25+
26+
# raises an error!
1227
class AccessDeniedApp
1328
class << self
1429
def error!(message, status=403)
@@ -68,6 +83,15 @@ def app
6883
last_response.body.should == '{"error":"rain!"}'
6984
end
7085

86+
it 'should be possible to return hash errors in json format' do
87+
@app ||= Rack::Builder.app do
88+
use Grape::Middleware::Error, :format => :json
89+
run ErrorHashApp
90+
end
91+
get '/'
92+
last_response.body.should == '{"error":"rain!","detail":"missing widget"}'
93+
end
94+
7195
it 'should be possible to specify a custom formatter' do
7296
@app ||= Rack::Builder.app do
7397
use Grape::Middleware::Error,

0 commit comments

Comments
 (0)