Skip to content

Commit f2f7ab0

Browse files
committed
Merge pull request #285 from dblock/rip-error-format
Removed error_format, will match the request content-type matched format.
2 parents 8710c60 + 5dbc6b6 commit f2f7ab0

File tree

9 files changed

+35
-48
lines changed

9 files changed

+35
-48
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#273](https://github.com/intridea/grape/pull/273): Disabled formatting via `serializable_hash` and added support for `format :serializable_hash` in API settings - [@dblock](https://github.com/dblock).
88
* [#277](https://github.com/intridea/grape/pull/277): Added a DSL to declare `formatter` in API settings - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
99
* [#284](https://github.com/intridea/grape/pull/284): Added a DSL to declare `error_formatter` in API settings - [@dblock](https://github.com/dblock).
10+
* [#285](https://github.com/intridea/grape/pull/285): Removed `error_format` from API settings, now matches request format - [@dblock](https://github.com/dblock).
1011
* Your contribution here.
1112

1213
0.2.2

README.markdown

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,7 @@ class Twitter::API < Grape::API
492492
end
493493
```
494494

495-
The error format can be specified using `error_format`. Available formats are
496-
`:json`, `:xml` and `:txt` (default).
497-
498-
``` ruby
499-
class Twitter::API < Grape::API
500-
error_format :json
501-
end
502-
```
495+
The error format will match the request format. See "Content-Types" below.
503496

504497
Custom error formatters for existing and additional types can be defined with a proc.
505498

@@ -519,7 +512,6 @@ module CustomFormatter
519512
end
520513

521514
class Twitter::API < Grape::API
522-
error_format :custom
523515
error_formatter :custom, CustomFormatter
524516
end
525517
```

lib/grape/api.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ def error_formatter(format, new_formatter)
139139
settings.imbue(:error_formatters, format.to_sym => new_formatter)
140140
end
141141

142-
# Specify the format for error messages.
143-
# May be `:json` or `:txt` (default).
144-
def error_format(new_format = nil)
145-
new_format ? set(:error_format, new_format.to_sym) : settings[:error_format]
146-
end
147-
148142
# Specify additional content-types, e.g.:
149143
# content_type :xls, 'application/vnd.ms-excel'
150144
def content_type(key, val)

lib/grape/endpoint.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ def build_middleware
377377
:default_status => settings[:default_error_status] || 403,
378378
:rescue_all => settings[:rescue_all],
379379
:rescued_errors => aggregate_setting(:rescued_errors),
380-
:format => settings[:error_format] || :txt,
381380
:error_formatters => settings[:error_formatters],
382381
:rescue_options => settings[:rescue_options],
383382
:rescue_handlers => merged_setting(:rescue_handlers)

lib/grape/middleware/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def content_types
5858
end
5959

6060
def content_type
61-
content_types[options[:format]] || 'text/html'
61+
content_types[env['api.format'] || options[:format]] || 'text/html'
6262
end
6363

6464
def mime_types

lib/grape/middleware/error.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ def rack_response(message, status = options[:default_status], headers = { 'Conte
6060
end
6161

6262
def format_message(message, backtrace, status)
63-
formatter = Grape::ErrorFormatter::Base.formatter_for(options[:format], options)
64-
throw :error, :status => 406, :message => "The requested format #{options[:format]} is not supported." unless formatter
63+
format = env['api.format'] || options[:format]
64+
formatter = Grape::ErrorFormatter::Base.formatter_for(format, options)
65+
throw :error, :status => 406, :message => "The requested format #{format} is not supported." unless formatter
6566
formatter.call(message, backtrace, options)
6667
end
6768

spec/grape/api_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def subject.enable_root_route!
456456
end
457457

458458
it 'should set content type for error' do
459-
subject.error_format :json
459+
subject.format :json
460460
subject.get('/error') { error!('error in json', 500) }
461461
get '/error.json'
462462
last_response.headers['Content-Type'].should eql 'application/json'
@@ -868,10 +868,10 @@ class CommunicationError < RuntimeError; end
868868
end
869869
end
870870

871-
describe ".error_format" do
871+
describe ".format for error" do
872872
it 'should rescue all errors and return :txt' do
873873
subject.rescue_from :all
874-
subject.error_format :txt
874+
subject.format :txt
875875
subject.get '/exception' do
876876
raise "rain!"
877877
end
@@ -881,7 +881,7 @@ class CommunicationError < RuntimeError; end
881881

882882
it 'should rescue all errors and return :txt with backtrace' do
883883
subject.rescue_from :all, :backtrace => true
884-
subject.error_format :txt
884+
subject.format :txt
885885
subject.get '/exception' do
886886
raise "rain!"
887887
end
@@ -910,7 +910,7 @@ def self.call(message, backtrace, options)
910910

911911
it 'should rescue all errors and return :json' do
912912
subject.rescue_from :all
913-
subject.error_format :json
913+
subject.format :json
914914
subject.get '/exception' do
915915
raise "rain!"
916916
end
@@ -919,7 +919,7 @@ def self.call(message, backtrace, options)
919919
end
920920
it 'should rescue all errors and return :json with backtrace' do
921921
subject.rescue_from :all, :backtrace => true
922-
subject.error_format :json
922+
subject.format :json
923923
subject.get '/exception' do
924924
raise "rain!"
925925
end
@@ -929,15 +929,15 @@ def self.call(message, backtrace, options)
929929
json["backtrace"].length.should > 0
930930
end
931931
it 'should rescue error! and return txt' do
932-
subject.error_format :txt
932+
subject.format :txt
933933
subject.get '/error' do
934934
error!("Access Denied", 401)
935935
end
936936
get '/error'
937937
last_response.body.should eql "Access Denied"
938938
end
939939
it 'should rescue error! and return json' do
940-
subject.error_format :json
940+
subject.format :json
941941
subject.get '/error' do
942942
error!("Access Denied", 401)
943943
end

spec/grape/validations/presence_spec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class API < Grape::API
1919
post do
2020
{:ret => params[:id]}
2121
end
22-
22+
2323
params do
2424
requires :name, :company
2525
end
@@ -52,7 +52,7 @@ class API < Grape::API
5252
end
5353
end
5454
end
55-
55+
5656
def app
5757
ValidationsSpec::PresenceValidatorSpec::API
5858
end
@@ -62,30 +62,30 @@ def app
6262
last_response.status.should == 200
6363
last_response.body.should == "All the bacon"
6464
end
65-
65+
6666
it 'validates id' do
6767
post('/')
6868
last_response.status.should == 400
69-
last_response.body.should == "missing parameter: id"
70-
69+
last_response.body.should == '{"error":"missing parameter: id"}'
70+
7171
post('/', {}, 'rack.input' => StringIO.new('{"id" : "a56b"}'))
72-
last_response.body.should == 'invalid parameter: id'
72+
last_response.body.should == '{"error":"invalid parameter: id"}'
7373
last_response.status.should == 400
74-
74+
7575
post('/', {}, 'rack.input' => StringIO.new('{"id" : 56}'))
7676
last_response.body.should == '{"ret":56}'
7777
last_response.status.should == 201
7878
end
79-
79+
8080
it 'validates name, company' do
8181
get('/')
8282
last_response.status.should == 400
83-
last_response.body.should == "missing parameter: name"
84-
83+
last_response.body.should == '{"error":"missing parameter: name"}'
84+
8585
get('/', :name => "Bob")
8686
last_response.status.should == 400
87-
last_response.body.should == "missing parameter: company"
88-
87+
last_response.body.should == '{"error":"missing parameter: company"}'
88+
8989
get('/', :name => "Bob", :company => "TestCorp")
9090
last_response.status.should == 200
9191
last_response.body.should == "Hello"
@@ -94,11 +94,11 @@ def app
9494
it 'validates nested parameters' do
9595
get('/nested')
9696
last_response.status.should == 400
97-
last_response.body.should == "missing parameter: first_name"
97+
last_response.body.should == '{"error":"missing parameter: first_name"}'
9898

9999
get('/nested', :user => {:first_name => "Billy"})
100100
last_response.status.should == 400
101-
last_response.body.should == "missing parameter: last_name"
101+
last_response.body.should == '{"error":"missing parameter: last_name"}'
102102

103103
get('/nested', :user => {:first_name => "Billy", :last_name => "Bob"})
104104
last_response.status.should == 200
@@ -108,27 +108,27 @@ def app
108108
it 'validates triple nested parameters' do
109109
get('/nested_triple')
110110
last_response.status.should == 400
111-
last_response.body.should == "missing parameter: admin_name"
111+
last_response.body.should == '{"error":"missing parameter: admin_name"}'
112112

113113
get('/nested_triple', :user => {:first_name => "Billy"})
114114
last_response.status.should == 400
115-
last_response.body.should == "missing parameter: admin_name"
115+
last_response.body.should == '{"error":"missing parameter: admin_name"}'
116116

117117
get('/nested_triple', :admin => {:super => {:first_name => "Billy"}})
118118
last_response.status.should == 400
119-
last_response.body.should == "missing parameter: admin_name"
119+
last_response.body.should == '{"error":"missing parameter: admin_name"}'
120120

121121
get('/nested_triple', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}})
122122
last_response.status.should == 400
123-
last_response.body.should == "missing parameter: admin_name"
123+
last_response.body.should == '{"error":"missing parameter: admin_name"}'
124124

125125
get('/nested_triple', :admin => {:super => {:user => {:first_name => "Billy"}}})
126126
last_response.status.should == 400
127-
last_response.body.should == "missing parameter: admin_name"
127+
last_response.body.should == '{"error":"missing parameter: admin_name"}'
128128

129129
get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy"}}})
130130
last_response.status.should == 400
131-
last_response.body.should == "missing parameter: last_name"
131+
last_response.body.should == '{"error":"missing parameter: last_name"}'
132132

133133
get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}}})
134134
last_response.status.should == 200

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
33
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
44

5-
$stdout = StringIO.new
5+
# $stdout = StringIO.new
66

77
require 'grape'
88

0 commit comments

Comments
 (0)