@@ -38,80 +38,82 @@ def call(env)
3838 def app
3939 @app
4040 end
41-
42- it 'should set the message appropriately ' do
41+
42+ it 'should not trap errors by default ' do
4343 @app ||= Rack ::Builder . app do
4444 use Grape ::Middleware ::Error
4545 run ExceptionApp
4646 end
47- get '/'
48- last_response . body . should == "rain!"
47+ lambda { get '/' } . should raise_error
4948 end
5049
51- it 'should default to a 403 status' do
52- @app ||= Rack ::Builder . app do
53- use Grape ::Middleware ::Error
54- run ExceptionApp
50+ context 'with rescue_all set to true' do
51+ it 'should set the message appropriately' do
52+ @app ||= Rack ::Builder . app do
53+ use Grape ::Middleware ::Error , :rescue_all => true
54+ run ExceptionApp
55+ end
56+ get '/'
57+ last_response . body . should == "rain!"
5558 end
56- get '/'
57- last_response . status . should == 403
58- end
5959
60- it 'should be possible to specify a different default status code' do
61- @app ||= Rack ::Builder . app do
62- use Grape ::Middleware ::Error , :default_status => 500
63- run ExceptionApp
64- end
65- get '/'
66- last_response . status . should == 500
67- end
68-
69- it 'should be possible to disable exception trapping' do
70- @app ||= Rack ::Builder . app do
71- use Grape ::Middleware ::Error , :rescue => false
72- run ExceptionApp
60+ it 'should default to a 403 status' do
61+ @app ||= Rack ::Builder . app do
62+ use Grape ::Middleware ::Error , :rescue_all => true
63+ run ExceptionApp
64+ end
65+ get '/'
66+ last_response . status . should == 403
7367 end
74- lambda { get '/' } . should raise_error
75- end
7668
77- it 'should be possible to return errors in json format' do
78- @app ||= Rack ::Builder . app do
79- use Grape ::Middleware ::Error , :format => :json
80- run ExceptionApp
69+ it 'should be possible to specify a different default status code' do
70+ @app ||= Rack ::Builder . app do
71+ use Grape ::Middleware ::Error , :rescue_all => true , :default_status => 500
72+ run ExceptionApp
73+ end
74+ get '/'
75+ last_response . status . should == 500
8176 end
82- get '/'
83- last_response . body . should == '{"error":"rain!"}'
84- end
85-
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
77+
78+ it ' should be possible to return errors in json format' do
79+ @app ||= Rack :: Builder . app do
80+ use Grape :: Middleware :: Error , :rescue_all => true , :format => :json
81+ run ExceptionApp
82+ end
83+ get '/'
84+ last_response . body . should == '{"error":"rain!"}'
9085 end
91- get '/'
92- last_response . body . should == '{"error":"rain!","detail":"missing widget"}'
93- end
9486
95- it 'should be possible to specify a custom formatter' do
96- @app ||= Rack ::Builder . app do
97- use Grape ::Middleware ::Error ,
98- :format => :custom ,
99- :formatters => {
100- :custom => lambda { |message , backtrace | { :custom_formatter => message } }
101- }
102- run ExceptionApp
87+ it 'should be possible to return hash errors in json format' do
88+ @app ||= Rack ::Builder . app do
89+ use Grape ::Middleware ::Error , :rescue_all => true , :format => :json
90+ run ErrorHashApp
91+ end
92+ get '/'
93+ last_response . body . should == '{"error":"rain!","detail":"missing widget"}'
10394 end
104- get '/'
105- last_response . body . should == '{:custom_formatter=>"rain!"}'
106- end
107-
108- it 'should not trap regular error! codes' do
109- @app ||= Rack ::Builder . app do
110- use Grape ::Middleware ::Error
111- run AccessDeniedApp
95+
96+ it 'should be possible to specify a custom formatter' do
97+ @app ||= Rack ::Builder . app do
98+ use Grape ::Middleware ::Error ,
99+ :rescue_all => true ,
100+ :format => :custom ,
101+ :formatters => {
102+ :custom => lambda { |message , backtrace | { :custom_formatter => message } }
103+ }
104+ run ExceptionApp
105+ end
106+ get '/'
107+ last_response . body . should == '{:custom_formatter=>"rain!"}'
112108 end
113- get '/'
114- last_response . status . should == 401
109+
110+ it 'should not trap regular error! codes' do
111+ @app ||= Rack ::Builder . app do
112+ use Grape ::Middleware ::Error
113+ run AccessDeniedApp
114+ end
115+ get '/'
116+ last_response . status . should == 401
117+ end
115118 end
116-
117119end
0 commit comments