Skip to content

Commit

Permalink
Enable to use custom helpers in error middleware
Browse files Browse the repository at this point in the history
Closes #438
  • Loading branch information
namusyaka authored and dblock committed Dec 26, 2015
1 parent 6f72077 commit 951ae51
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
0.14.1 (Next)
=============

* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
#### Features

* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
* [#1232](https://github.com/ruby-grape/grape/pull/1232): Helpers are now available inside `rescue_from` - [@namusyaka](https://github.com/namusyaka).
* Your contribution here.

#### Fixes

* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).

0.14.0 (12/07/2015)
===================

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,23 @@ rescue_from RuntimeError, rescue_subclasses: false do |e|
end
```

Helpers are also available inside `rescue_from`.

```ruby
class Twitter::API < Grape::API
format :json
helpers do
def server_error!
error!({ error: 'Server error.' }, 500, { 'Content-Type' => 'text/error' })
end
end

rescue_from :all do |e|
server_error!
end
end
```

The `rescue_from` block must return a `Rack::Response` object, call `error!` or re-raise an exception.

#### Unrescuable Exceptions
Expand Down
15 changes: 15 additions & 0 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def default_options
def call!(env)
@env = env

inject_helpers!

begin
error_response(catch(:error) do
return @app.call(@env)
Expand Down Expand Up @@ -59,6 +61,19 @@ def exec_handler(e, &handler)
end
end

def inject_helpers!
return if helpers_available?
endpoint = @env['api.endpoint']
self.class.instance_eval do
include endpoint.send(:helpers)
end if endpoint.is_a?(Grape::Endpoint)
@helpers = true
end

def helpers_available?
@helpers
end

def error!(message, status = options[:default_status], headers = {}, backtrace = [])
headers = headers.reverse_merge(Grape::Http::Headers::CONTENT_TYPE => content_type)
rack_response(format_message(message, backtrace), status, headers)
Expand Down
14 changes: 14 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,20 @@ def three
expect { get '/exception' }.to raise_error(RuntimeError, 'rain!')
end

it 'uses custom helpers defined by using #helpers method' do
subject.helpers do
def custom_error!(name)
error! "hello #{name}"
end
end
subject.rescue_from(ArgumentError) { custom_error! :bob }
subject.get '/custom_error' do
fail ArgumentError
end
get '/custom_error'
expect(last_response.body).to eq 'hello bob'
end

it 'rescues all errors if rescue_from :all is called' do
subject.rescue_from :all
subject.get '/exception' do
Expand Down

0 comments on commit 951ae51

Please sign in to comment.