Skip to content

Commit 6e418d3

Browse files
vad4msiudblock
authored andcommitted
Fixed default_error_formatter which takes a format symbol.
1 parent e7c44e0 commit 6e418d3

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Next Release
55

66
* [#475](https://github.com/intridea/grape/pull/475): Added support for the `:jsonapi`, `application/vnd.api+json` media type registered at http://jsonapi.org - [@bcm](https://github.com/bcm).
77
* [#471](https://github.com/intridea/grape/issues/471): Added parameter validator for a list of allowed values - [@vickychijwani](https://github.com/vickychijwani).
8+
9+
#### Fixes
10+
11+
* [#477](https://github.com/intridea/grape/pull/477): Fixed `default_error_formatter` which takes a format symbol - [@vad4msiu](https://github.com/vad4msiu).
812
* Your contribution here.
913

1014
0.6.0 (9/16/2013)

lib/grape/api.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,13 @@ def parser(content_type, new_parser)
147147
end
148148

149149
# Specify a default error formatter.
150-
def default_error_formatter(new_formatter = nil)
151-
new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter]
150+
def default_error_formatter(new_formatter_name = nil)
151+
if new_formatter_name
152+
new_formatter = Grape::ErrorFormatter::Base.formatter_for(new_formatter_name, {})
153+
set(:default_error_formatter, new_formatter)
154+
else
155+
settings[:default_error_formatter]
156+
end
152157
end
153158

154159
def error_formatter(format, options)

spec/grape/api_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -2191,4 +2191,17 @@ def before
21912191
end
21922192
end
21932193
end
2194+
2195+
context 'with json default_error_formatter' do
2196+
it 'returns json error' do
2197+
subject.content_type :json, "application/json"
2198+
subject.default_error_formatter :json
2199+
subject.get '/something' do
2200+
'foo'
2201+
end
2202+
get '/something'
2203+
last_response.status.should == 406
2204+
last_response.body.should == "{\"error\":\"The requested format 'txt' is not supported.\"}"
2205+
end
2206+
end
21942207
end

0 commit comments

Comments
 (0)