Skip to content

Commit

Permalink
Merge pull request #698 from dspaeth-faber/set_endpoint_status_for_er…
Browse files Browse the repository at this point in the history
…rors

Set status for endpoints when an error occours
  • Loading branch information
dblock committed Jul 30, 2014
2 parents 4efcc0b + b584c48 commit 52b63db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
============

* [#687](https://github.com/intridea/grape/pull/687): Fix: `mutually_exclusive` and `exactly_one_of` validation error messages now label parameters as strings, consistently with `requires` and `optional` - [@dblock](https://github.com/dblock).
* [#698](https://github.com/intridea/grape/pull/698): `error!` sets `status` for `Endpoint` to [@dspaeth-faber](https://github.com/dspaeth-faber).
* Your contribution here.

0.8.0 (7/10/2014)
Expand Down
8 changes: 5 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def initialize(settings, options = {}, &block)
require_option(options, :method)

@settings = settings
@settings[:default_error_status] ||= 500

@options = options

@options[:path] = Array(options[:path])
Expand Down Expand Up @@ -225,8 +227,8 @@ def version
# @param message [String] The message to display.
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set.
def error!(message, status = nil, headers = nil)
status = settings[:default_error_status] unless status
throw :error, message: message, status: status, headers: headers
self.status(status || settings[:default_error_status])
throw :error, message: message, status: self.status, headers: headers
end

# Redirect to a new url.
Expand Down Expand Up @@ -435,7 +437,7 @@ def build_middleware
b.use Grape::Middleware::Error,
format: settings[:format],
content_types: settings[:content_types],
default_status: settings[:default_error_status] || 500,
default_status: settings[:default_error_status],
rescue_all: settings[:rescue_all],
default_error_formatter: settings[:default_error_formatter],
error_formatters: settings[:error_formatters],
Expand Down
13 changes: 13 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,19 @@ def app
expect(last_response.status).to eq(403)
expect(last_response.headers['X-Custom']).to eq('value')
end

it 'sets the status code for the endpoint' do
memoized_endpoint = nil

subject.get '/hey' do
memoized_endpoint = self
error!({ 'dude' => 'rad' }, 403, 'X-Custom' => 'value')
end

get '/hey.json'

expect(memoized_endpoint.status).to eq(403)
end
end

describe '#redirect' do
Expand Down

0 comments on commit 52b63db

Please sign in to comment.