Skip to content

Commit

Permalink
The default status code returned from error\! has been changed from 4…
Browse files Browse the repository at this point in the history
…03 to 500.
  • Loading branch information
dblock committed Dec 4, 2013
1 parent 35a6180 commit 6b35a04
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next Release
* [#510](https://github.com/intridea/grape/pull/510): Support lambda-based default values for params - [@myitcv](https://github.com/myitcv).
* [#511](https://github.com/intridea/grape/pull/511): Add `required` option for OAuth2 middleware - [@bcm](https://github.com/bcm).
* [#520](https://github.com/intridea/grape/pull/520): Use `default_error_status` to specify the default status code returned from `error!` - [@salimane](https://github.com/salimane).
* [#525](https://github.com/intridea/grape/pull/525): The default status code returned from `error!` has been changed from 403 to 500 - [@dblock](https://github.com/dblock).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ error!({ "error" => "unexpected error", "detail" => "missing widget" }, 500)

### Default Error HTTP Status Code

By default Grape returns a 403 status code from `error!`. You can change this with `default_error_status`.
By default Grape returns a 500 status code from `error!`. You can change this with `default_error_status`.

``` ruby
class API < Grape::API
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def version
# end user with the specified message.
#
# @param message [String] The message to display.
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 403 if not set.
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set.
def error!(message, status = nil)
status = settings[:default_error_status] unless status
throw :error, message: message, status: status
Expand Down Expand Up @@ -406,7 +406,7 @@ def build_middleware
b.use Rack::Head
b.use Grape::Middleware::Error,
format: settings[:format],
default_status: settings[:default_error_status] || 403,
default_status: settings[:default_error_status] || 500,
rescue_all: settings[:rescue_all],
rescued_errors: aggregate_setting(:rescued_errors),
default_error_formatter: settings[:default_error_formatter],
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Middleware
class Error < Base
def default_options
{
default_status: 403, # default status returned on error
default_status: 500, # default status returned on error
default_message: "",
format: :txt,
formatters: {},
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ def three
raise "rain!"
end
get '/exception'
last_response.status.should eql 403
last_response.status.should eql 500
end

it 'rescues only certain errors if rescue_from is called with specific errors' do
Expand All @@ -1002,7 +1002,7 @@ def three
subject.get('/unrescued') { raise "beefcake" }

get '/rescued'
last_response.status.should eql 403
last_response.status.should eql 500

lambda { get '/unrescued' }.should raise_error
end
Expand Down Expand Up @@ -1477,7 +1477,7 @@ def self.call(object, env)
raise "rain!"
end
get '/exception'
last_response.status.should eql 403
last_response.status.should eql 500
end
it 'uses the default error status in error!' do
subject.rescue_from :all
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def app
end

get '/hey'
last_response.status.should == 403
last_response.status.should == 500
last_response.body.should == "This is not valid."
end

Expand Down
4 changes: 2 additions & 2 deletions spec/grape/middleware/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def app
last_response.body.should == 'Awesome stuff.'
end

it 'defaults to a 403 status' do
it 'defaults to a 500 status' do
ErrApp.error = {}
get '/'
last_response.status.should == 403
last_response.status.should == 500
end

it 'has a default message' do
Expand Down
8 changes: 4 additions & 4 deletions spec/grape/middleware/exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(env)
# raises a hash error
class ErrorHashApp
class << self
def error!(message, status = 403)
def error!(message, status)
throw :error, message: { error: message, detail: "missing widget" }, status: status
end

Expand All @@ -28,7 +28,7 @@ def call(env)
# raises an error!
class AccessDeniedApp
class << self
def error!(message, status = 403)
def error!(message, status)
throw :error, message: message, status: status
end

Expand Down Expand Up @@ -70,13 +70,13 @@ def call(env)
last_response.body.should == "rain!"
end

it 'defaults to a 403 status' do
it 'defaults to a 500 status' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error, rescue_all: true
run ExceptionApp
end
get '/'
last_response.status.should == 403
last_response.status.should == 500
end

it 'is possible to specify a different default status code' do
Expand Down

0 comments on commit 6b35a04

Please sign in to comment.