Skip to content

Commit 844b6ac

Browse files
jrforrestdblock
authored andcommitted
Fix JSON error response when calling error! with non-Strings.
1 parent a81967a commit 844b6ac

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.14.1 (Next)
22
=============
33

4+
* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
45
* Your contribution here.
56

67
0.14.0 (12/17/2015)

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,12 @@ You can abort the execution of an API method by raising errors with `error!`.
15431543
error! 'Access Denied', 401
15441544
```
15451545

1546+
Anything that responds to `#to_s` can be given as a first argument to `error!`.
1547+
1548+
```ruby
1549+
error! :not_found, 404
1550+
```
1551+
15461552
You can also return JSON formatted objects by raising error! and passing a hash
15471553
instead of a message.
15481554

lib/grape/error_formatter/json.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ module ErrorFormatter
33
module Json
44
class << self
55
def call(message, backtrace, options = {}, env = nil)
6-
message = Grape::ErrorFormatter::Base.present(message, env)
6+
result = wrap_message(Grape::ErrorFormatter::Base.present(message, env))
77

8-
result = message.is_a?(String) ? { error: message } : message
98
if (options[:rescue_options] || {})[:backtrace] && backtrace && !backtrace.empty?
109
result = result.merge(backtrace: backtrace)
1110
end
1211
MultiJson.dump(result)
1312
end
13+
14+
private
15+
16+
def wrap_message(message)
17+
if message.is_a?(Exceptions::ValidationErrors) || message.is_a?(Hash)
18+
message
19+
else
20+
{ error: message }
21+
end
22+
end
1423
end
1524
end
1625
end

spec/grape/api_spec.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,13 +1668,23 @@ def self.call(message, _backtrace, _option, _env)
16681668
get '/error'
16691669
expect(last_response.body).to eql 'Access Denied'
16701670
end
1671-
it 'rescues error! and return json' do
1672-
subject.format :json
1673-
subject.get '/error' do
1674-
error!('Access Denied', 401)
1671+
context 'with json format' do
1672+
before { subject.format :json }
1673+
1674+
it 'rescues error! called with a string and returns json' do
1675+
subject.get('/error') { error!(:failure, 401) }
1676+
end
1677+
it 'rescues error! called with a symbol and returns json' do
1678+
subject.get('/error') { error!(:failure, 401) }
1679+
end
1680+
it 'rescues error! called with a hash and returns json' do
1681+
subject.get('/error') { error!({ error: :failure }, 401) }
1682+
end
1683+
1684+
after do
1685+
get '/error'
1686+
expect(last_response.body).to eql('{"error":"failure"}')
16751687
end
1676-
get '/error'
1677-
expect(last_response.body).to eql '{"error":"Access Denied"}'
16781688
end
16791689
end
16801690

0 commit comments

Comments
 (0)