@@ -50,113 +50,146 @@ def call(_env)
50
50
end
51
51
end
52
52
53
- attr_reader :app
53
+ def app
54
+ subject
55
+ end
54
56
55
- it 'does not trap errors by default' do
56
- @app ||= Rack ::Builder . app do
57
- use Spec ::Support ::EndpointFaker
58
- use Grape ::Middleware ::Error
59
- run ExceptionSpec ::ExceptionApp
57
+ context 'with defaults' do
58
+ subject do
59
+ Rack ::Builder . app do
60
+ use Spec ::Support ::EndpointFaker
61
+ use Grape ::Middleware ::Error
62
+ run ExceptionSpec ::ExceptionApp
63
+ end
64
+ end
65
+ it 'does not trap errors by default' do
66
+ expect { get '/' } . to raise_error ( RuntimeError , 'rain!' )
60
67
end
61
- expect { get '/' } . to raise_error ( RuntimeError , 'rain!' )
62
68
end
63
69
64
- context 'with rescue_all set to true ' do
65
- it 'sets the message appropriately' do
66
- @app ||= Rack ::Builder . app do
70
+ context 'with rescue_all' do
71
+ subject do
72
+ Rack ::Builder . app do
67
73
use Spec ::Support ::EndpointFaker
68
74
use Grape ::Middleware ::Error , rescue_all : true
69
75
run ExceptionSpec ::ExceptionApp
70
76
end
77
+ end
78
+ it 'sets the message appropriately' do
71
79
get '/'
72
80
expect ( last_response . body ) . to eq ( 'rain!' )
73
81
end
74
-
75
82
it 'defaults to a 500 status' do
76
- @app ||= Rack ::Builder . app do
77
- use Spec ::Support ::EndpointFaker
78
- use Grape ::Middleware ::Error , rescue_all : true
79
- run ExceptionSpec ::ExceptionApp
80
- end
81
83
get '/'
82
84
expect ( last_response . status ) . to eq ( 500 )
83
85
end
86
+ end
84
87
85
- it 'is possible to specify a different default status code' do
86
- @app ||= Rack ::Builder . app do
88
+ context do
89
+ subject do
90
+ Rack ::Builder . app do
87
91
use Spec ::Support ::EndpointFaker
88
92
use Grape ::Middleware ::Error , rescue_all : true , default_status : 500
89
93
run ExceptionSpec ::ExceptionApp
90
94
end
95
+ end
96
+ it 'is possible to specify a different default status code' do
91
97
get '/'
92
98
expect ( last_response . status ) . to eq ( 500 )
93
99
end
100
+ end
94
101
95
- it 'is possible to return errors in json format' do
96
- @app ||= Rack ::Builder . app do
102
+ context do
103
+ subject do
104
+ Rack ::Builder . app do
97
105
use Spec ::Support ::EndpointFaker
98
106
use Grape ::Middleware ::Error , rescue_all : true , format : :json
99
107
run ExceptionSpec ::ExceptionApp
100
108
end
109
+ end
110
+ it 'is possible to return errors in json format' do
101
111
get '/'
102
112
expect ( last_response . body ) . to eq ( '{"error":"rain!"}' )
103
113
end
114
+ end
104
115
105
- it 'is possible to return hash errors in json format' do
106
- @app ||= Rack ::Builder . app do
116
+ context do
117
+ subject do
118
+ Rack ::Builder . app do
107
119
use Spec ::Support ::EndpointFaker
108
120
use Grape ::Middleware ::Error , rescue_all : true , format : :json
109
121
run ExceptionSpec ::ErrorHashApp
110
122
end
123
+ end
124
+ it 'is possible to return hash errors in json format' do
111
125
get '/'
112
126
expect ( [ '{"error":"rain!","detail":"missing widget"}' ,
113
127
'{"detail":"missing widget","error":"rain!"}' ] ) . to include ( last_response . body )
114
128
end
129
+ end
115
130
116
- it 'is possible to return errors in jsonapi format' do
117
- @app ||= Rack ::Builder . app do
131
+ context do
132
+ subject do
133
+ Rack ::Builder . app do
118
134
use Spec ::Support ::EndpointFaker
119
135
use Grape ::Middleware ::Error , rescue_all : true , format : :jsonapi
120
136
run ExceptionSpec ::ExceptionApp
121
137
end
138
+ end
139
+ it 'is possible to return errors in jsonapi format' do
122
140
get '/'
123
141
expect ( last_response . body ) . to eq ( '{"error":"rain!"}' )
124
142
end
143
+ end
125
144
126
- it 'is possible to return hash errors in jsonapi format' do
127
- @app ||= Rack ::Builder . app do
145
+ context do
146
+ subject do
147
+ Rack ::Builder . app do
128
148
use Spec ::Support ::EndpointFaker
129
149
use Grape ::Middleware ::Error , rescue_all : true , format : :jsonapi
130
150
run ExceptionSpec ::ErrorHashApp
131
151
end
152
+ end
153
+
154
+ it 'is possible to return hash errors in jsonapi format' do
132
155
get '/'
133
156
expect ( [ '{"error":"rain!","detail":"missing widget"}' ,
134
157
'{"detail":"missing widget","error":"rain!"}' ] ) . to include ( last_response . body )
135
158
end
159
+ end
136
160
137
- it 'is possible to return errors in xml format' do
138
- @app ||= Rack ::Builder . app do
161
+ context do
162
+ subject do
163
+ Rack ::Builder . app do
139
164
use Spec ::Support ::EndpointFaker
140
165
use Grape ::Middleware ::Error , rescue_all : true , format : :xml
141
166
run ExceptionSpec ::ExceptionApp
142
167
end
168
+ end
169
+ it 'is possible to return errors in xml format' do
143
170
get '/'
144
171
expect ( last_response . body ) . to eq ( "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n <error>\n <message>rain!</message>\n </error>\n " )
145
172
end
173
+ end
146
174
147
- it 'is possible to return hash errors in xml format' do
148
- @app ||= Rack ::Builder . app do
175
+ context do
176
+ subject do
177
+ Rack ::Builder . app do
149
178
use Spec ::Support ::EndpointFaker
150
179
use Grape ::Middleware ::Error , rescue_all : true , format : :xml
151
180
run ExceptionSpec ::ErrorHashApp
152
181
end
182
+ end
183
+ it 'is possible to return hash errors in xml format' do
153
184
get '/'
154
185
expect ( [ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n <error>\n <detail>missing widget</detail>\n <error>rain!</error>\n </error>\n " ,
155
186
"<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n <error>\n <error>rain!</error>\n <detail>missing widget</detail>\n </error>\n " ] ) . to include ( last_response . body )
156
187
end
188
+ end
157
189
158
- it 'is possible to specify a custom formatter' do
159
- @app ||= Rack ::Builder . app do
190
+ context do
191
+ subject do
192
+ Rack ::Builder . app do
160
193
use Spec ::Support ::EndpointFaker
161
194
use Grape ::Middleware ::Error ,
162
195
rescue_all : true ,
@@ -168,71 +201,90 @@ def call(_env)
168
201
}
169
202
run ExceptionSpec ::ExceptionApp
170
203
end
204
+ end
205
+ it 'is possible to specify a custom formatter' do
171
206
get '/'
172
207
expect ( last_response . body ) . to eq ( '{:custom_formatter=>"rain!"}' )
173
208
end
209
+ end
174
210
175
- it 'does not trap regular error! codes' do
176
- @app ||= Rack ::Builder . app do
211
+ context do
212
+ subject do
213
+ Rack ::Builder . app do
177
214
use Spec ::Support ::EndpointFaker
178
215
use Grape ::Middleware ::Error
179
216
run ExceptionSpec ::AccessDeniedApp
180
217
end
218
+ end
219
+ it 'does not trap regular error! codes' do
181
220
get '/'
182
221
expect ( last_response . status ) . to eq ( 401 )
183
222
end
223
+ end
184
224
185
- it 'responds to custom Grape exceptions appropriately' do
186
- @app ||= Rack ::Builder . app do
225
+ context do
226
+ subject do
227
+ Rack ::Builder . app do
187
228
use Spec ::Support ::EndpointFaker
188
229
use Grape ::Middleware ::Error , rescue_all : false
189
230
run ExceptionSpec ::CustomErrorApp
190
231
end
191
-
232
+ end
233
+ it 'responds to custom Grape exceptions appropriately' do
192
234
get '/'
193
235
expect ( last_response . status ) . to eq ( 400 )
194
236
expect ( last_response . body ) . to eq ( 'failed validation' )
195
237
end
238
+ end
196
239
197
- context 'with rescue_options :backtrace and :exception set to true' do
198
- it 'is possible to return the backtrace and the original exception in json format' do
199
- @app ||= Rack ::Builder . app do
200
- use Spec ::Support ::EndpointFaker
201
- use Grape ::Middleware ::Error ,
202
- rescue_all : true ,
203
- format : :json ,
204
- rescue_options : { backtrace : true , original_exception : true }
205
- run ExceptionSpec ::ExceptionApp
206
- end
207
- get '/'
208
- expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original_exception' , 'RuntimeError' )
209
- end
210
-
211
- it 'is possible to return the backtrace and the original exception in xml format' do
212
- @app ||= Rack ::Builder . app do
213
- use Spec ::Support ::EndpointFaker
214
- use Grape ::Middleware ::Error ,
215
- rescue_all : true ,
216
- format : :xml ,
217
- rescue_options : { backtrace : true , original_exception : true }
218
- run ExceptionSpec ::ExceptionApp
219
- end
220
- get '/'
221
- expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original-exception' , 'RuntimeError' )
222
- end
223
-
224
- it 'is possible to return the backtrace and the original exception in txt format' do
225
- @app ||= Rack ::Builder . app do
226
- use Spec ::Support ::EndpointFaker
227
- use Grape ::Middleware ::Error ,
228
- rescue_all : true ,
229
- format : :txt ,
230
- rescue_options : { backtrace : true , original_exception : true }
231
- run ExceptionSpec ::ExceptionApp
232
- end
233
- get '/'
234
- expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original exception' , 'RuntimeError' )
240
+ context 'with rescue_options :backtrace and :exception set to true' do
241
+ subject do
242
+ Rack ::Builder . app do
243
+ use Spec ::Support ::EndpointFaker
244
+ use Grape ::Middleware ::Error ,
245
+ rescue_all : true ,
246
+ format : :json ,
247
+ rescue_options : { backtrace : true , original_exception : true }
248
+ run ExceptionSpec ::ExceptionApp
235
249
end
236
250
end
251
+ it 'is possible to return the backtrace and the original exception in json format' do
252
+ get '/'
253
+ expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original_exception' , 'RuntimeError' )
254
+ end
255
+ end
256
+
257
+ context do
258
+ subject do
259
+ Rack ::Builder . app do
260
+ use Spec ::Support ::EndpointFaker
261
+ use Grape ::Middleware ::Error ,
262
+ rescue_all : true ,
263
+ format : :xml ,
264
+ rescue_options : { backtrace : true , original_exception : true }
265
+ run ExceptionSpec ::ExceptionApp
266
+ end
267
+ end
268
+ it 'is possible to return the backtrace and the original exception in xml format' do
269
+ get '/'
270
+ expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original-exception' , 'RuntimeError' )
271
+ end
272
+ end
273
+
274
+ context do
275
+ subject do
276
+ Rack ::Builder . app do
277
+ use Spec ::Support ::EndpointFaker
278
+ use Grape ::Middleware ::Error ,
279
+ rescue_all : true ,
280
+ format : :txt ,
281
+ rescue_options : { backtrace : true , original_exception : true }
282
+ run ExceptionSpec ::ExceptionApp
283
+ end
284
+ end
285
+ it 'is possible to return the backtrace and the original exception in txt format' do
286
+ get '/'
287
+ expect ( last_response . body ) . to include ( 'error' , 'rain!' , 'backtrace' , 'original exception' , 'RuntimeError' )
288
+ end
237
289
end
238
290
end
0 commit comments