Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed assignment default_error_formatter #477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ def parser(content_type, new_parser)
end

# Specify a default error formatter.
def default_error_formatter(new_formatter = nil)
new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter]
def default_error_formatter(new_formatter_name = nil)
if new_formatter_name
new_formatter = Grape::ErrorFormatter::Base.formatter_for(new_formatter_name, {})
set(:default_error_formatter, new_formatter)
else
settings[:default_error_formatter]
end
end

def error_formatter(format, options)
Expand Down
13 changes: 13 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2191,4 +2191,17 @@ def before
end
end
end

context 'with json default_error_formatter' do
it 'return json error' do
subject.content_type :json, "application/json"
subject.default_error_formatter :json
subject.get '/something' do
'foo'
end
get '/something'
last_response.status.should == 406
last_response.body.should == "{\"error\":\"The requested format 'txt' is not supported.\"}"
end
end
end