Skip to content

Commit a481a5b

Browse files
committed
Added options into the API model.
1 parent e3f5257 commit a481a5b

File tree

3 files changed

+99
-19
lines changed

3 files changed

+99
-19
lines changed

README.markdown

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,14 @@ Serialization takes place automatically. For more detailed usage information, pl
6666

6767
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.
6868

69-
It's possible to disable this behavior and pass exceptions further up the stack. This usually means displaying the web server's error page as a result.
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`.
7070

7171
class Twitter::API < Grape::API
72-
use Grape::Middleware::Error, :rescue => false
73-
...
74-
end
75-
76-
It's also common to change the error format to JSON.
72+
rescue_all_errors false
73+
error_format :json
74+
default_error_status 200
7775

78-
class Twitter::API < Grape::API
79-
use Grape::Middleware::Error, :format => :json
80-
...
81-
end
82-
83-
Finally, you can specify your own error formatter. The following example returns a custom error message in the JSON format.
84-
85-
class Twitter::API < Grape::API
86-
use Grape::Middleware::Error, :format => :custom, :formatters = { lambda { |message| { :custom => "the error message was: #{message}" } }
87-
...
76+
# api methods
8877
end
8978

9079
## Note on Patches/Pull Requests

lib/grape/api.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def set(key, value)
5050
def prefix(prefix = nil)
5151
prefix ? set(:root_prefix, prefix) : settings[:root_prefix]
5252
end
53-
53+
5454
# Specify an API version.
5555
#
5656
# @example API with legacy support.
@@ -79,6 +79,22 @@ def default_format(new_format = nil)
7979
new_format ? set(:default_format, new_format.to_sym) : settings[:default_format]
8080
end
8181

82+
# Specify the format for error messages.
83+
# May be `:json` or `:txt` (default).
84+
def error_format(new_format = nil)
85+
new_format ? set(:error_format, new_format.to_sym) : settings[:error_format]
86+
end
87+
88+
# Specify the default status code for errors.
89+
def default_error_status(new_status = nil)
90+
new_status ? set(:default_error_status, new_status) : settings[:default_error_status]
91+
end
92+
93+
# Specify whether to rescue all errors.
94+
def rescue_all_errors(new_value = true)
95+
set(:rescue_all_errors, new_value)
96+
end
97+
8298
# Add helper methods that will be accessible from any
8399
# endpoint within this namespace (and child namespaces).
84100
#
@@ -217,7 +233,7 @@ def nest(*blocks, &block)
217233

218234
def build_endpoint(&block)
219235
b = Rack::Builder.new
220-
b.use Grape::Middleware::Error
236+
b.use Grape::Middleware::Error, :default_status => settings[:default_error_status] || 403, :rescue => settings[:rescue_all_errors], :format => settings[:error_format] || :txt
221237
b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
222238
b.use Grape::Middleware::Prefixer, :prefix => prefix if prefix
223239
b.use Grape::Middleware::Versioner, :versions => (version if version.is_a?(Array)) if version

spec/grape/api_spec.rb

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def app; subject end
2020
last_response.status.should eql 404
2121
end
2222
end
23-
23+
2424
describe '.version' do
2525
it 'should set the API version' do
2626
subject.version 'v1'
@@ -471,4 +471,79 @@ def two
471471
last_response.status.should eql 200
472472
end
473473
end
474+
475+
describe ".rescue_all_errors" do
476+
it 'should not rescue all errors when rescue_all_errors is false' do
477+
subject.rescue_all_errors false
478+
subject.get '/exception' do
479+
raise "rain!"
480+
end
481+
lambda { get '/exception' }.should raise_error
482+
end
483+
it 'should rescue all errors' do
484+
subject.rescue_all_errors true
485+
subject.get '/exception' do
486+
raise "rain!"
487+
end
488+
get '/exception'
489+
last_response.status.should eql 403
490+
end
491+
end
492+
493+
describe ".error_format" do
494+
it 'should return :txt' do
495+
subject.rescue_all_errors true
496+
subject.error_format :txt
497+
subject.get '/exception' do
498+
raise "rain!"
499+
end
500+
get '/exception'
501+
last_response.body.should eql "rain!"
502+
end
503+
it 'should rescue all errors' do
504+
subject.rescue_all_errors true
505+
subject.error_format :json
506+
subject.get '/exception' do
507+
raise "rain!"
508+
end
509+
get '/exception'
510+
last_response.body.should eql '{:error=>"rain!"}'
511+
end
512+
it 'should rescue all errors' do
513+
subject.error_format :txt
514+
subject.get '/error' do
515+
error!("Access Denied", 401)
516+
end
517+
get '/error'
518+
last_response.body.should eql "Access Denied"
519+
end
520+
it 'should rescue all errors' do
521+
subject.error_format :json
522+
subject.get '/error' do
523+
error!("Access Denied", 401)
524+
end
525+
get '/error'
526+
last_response.body.should eql '{:error=>"Access Denied"}'
527+
end
528+
end
529+
530+
describe ".default_error_status" do
531+
it 'should allow setting default_error_status' do
532+
subject.rescue_all_errors true
533+
subject.default_error_status 200
534+
subject.get '/exception' do
535+
raise "rain!"
536+
end
537+
get '/exception'
538+
last_response.status.should eql 200
539+
end
540+
it 'should have a default error status' do
541+
subject.rescue_all_errors true
542+
subject.get '/exception' do
543+
raise "rain!"
544+
end
545+
get '/exception'
546+
last_response.status.should eql 403
547+
end
548+
end
474549
end

0 commit comments

Comments
 (0)