Skip to content

Commit fd31ae9

Browse files
committed
Unify behavior of default_error_status, ref #1177
1 parent eae2d14 commit fd31ae9

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
1919
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
2020
* [#1249](https://github.com/ruby-grape/grape/pull/1249): Don't fail even if invalid type value is passed to default validator - [@namusyaka](https://github.com/namusyaka).
21+
* [#1264](https://github.com/ruby-grape/grape/pull/1264): Unify behavior of default_error_status - [@namusyaka](https://github.com/namusyaka).
2122

2223
0.14.0 (12/07/2015)
2324
===================

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,8 @@ class API < Grape::API
16121612
end
16131613
```
16141614

1615+
Of course, `default_error_status` does not depend on the timing of route definition, so mounted application can also use the default error status code.
1616+
16151617
### Handling 404
16161618

16171619
For Grape to handle all the 404s for your API, it can be useful to use a catch-all.

UPGRADING.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
Upgrading Grape
22
===============
33

4+
#### Changes to behavior of `default_error_status`
5+
6+
The `default_error_status` method had been depending on the timing of route definition.
7+
8+
Currently, the difference of behaviors has been unified. The following code is treated as same meaning.
9+
10+
```ruby
11+
class A < Grape::API
12+
default_error_status 400
13+
end
14+
15+
class API < Grape::API
16+
mount A
17+
end
18+
19+
A.get '/error' do
20+
error!('error')
21+
end
22+
```
23+
24+
```ruby
25+
class B < Grape::API
26+
default_error_status 400
27+
get '/error' do
28+
error!('error')
29+
end
30+
end
31+
32+
class API < Grape::API
33+
mount B
34+
end
35+
```
36+
437
### Upgrading to >= 0.15.0
538

639
#### Changes to availability of `:with` option of `rescue_from` method

lib/grape/dsl/routing.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ def mount(mounts)
8787
change!
8888
end
8989

90+
app.endpoints.each do |endpoint|
91+
current_error_status = namespace_inheritable(:default_error_status) || 500
92+
endpoint.namespace_inheritable(:default_error_status, current_error_status)
93+
end if !app.is_a?(Proc) && app.ancestors.include?(Grape::API)
94+
9095
endpoints << Grape::Endpoint.new(
9196
in_setting,
9297
method: :any,

spec/grape/api_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,37 @@ def self.call(object, _env)
19331933
get '/exception'
19341934
expect(last_response.status).to eql 400
19351935
end
1936+
1937+
context 'with mounted api' do
1938+
before do
1939+
subject.version 'v1', using: :path
1940+
subject.default_error_status 400
1941+
end
1942+
1943+
it 'inherits default error status' do
1944+
api = Class.new(Grape::API)
1945+
subject.mount api
1946+
api.get('/error') do
1947+
error!('error!')
1948+
end
1949+
1950+
get '/v1/error'
1951+
expect(last_response.status).to eql 400
1952+
expect(last_response.body).to eq('error!')
1953+
end
1954+
1955+
it 'inherits default error status even if target route is registered before mounting self' do
1956+
api = Class.new(Grape::API)
1957+
api.get('/error') do
1958+
error!('error!')
1959+
end
1960+
subject.mount api
1961+
1962+
get '/v1/error'
1963+
expect(last_response.status).to eql 400
1964+
expect(last_response.body).to eq('error!')
1965+
end
1966+
end
19361967
end
19371968

19381969
context 'http_codes' do

0 commit comments

Comments
 (0)