Skip to content

Commit 951ae51

Browse files
namusyakadblock
authored andcommitted
Enable to use custom helpers in error middleware
Closes #438
1 parent 6f72077 commit 951ae51

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
0.14.1 (Next)
22
=============
33

4-
* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
5-
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
4+
#### Features
5+
66
* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
7+
* [#1232](https://github.com/ruby-grape/grape/pull/1232): Helpers are now available inside `rescue_from` - [@namusyaka](https://github.com/namusyaka).
78
* Your contribution here.
89

10+
#### Fixes
11+
12+
* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
13+
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
14+
915
0.14.0 (12/07/2015)
1016
===================
1117

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,23 @@ rescue_from RuntimeError, rescue_subclasses: false do |e|
17381738
end
17391739
```
17401740

1741+
Helpers are also available inside `rescue_from`.
1742+
1743+
```ruby
1744+
class Twitter::API < Grape::API
1745+
format :json
1746+
helpers do
1747+
def server_error!
1748+
error!({ error: 'Server error.' }, 500, { 'Content-Type' => 'text/error' })
1749+
end
1750+
end
1751+
1752+
rescue_from :all do |e|
1753+
server_error!
1754+
end
1755+
end
1756+
```
1757+
17411758
The `rescue_from` block must return a `Rack::Response` object, call `error!` or re-raise an exception.
17421759

17431760
#### Unrescuable Exceptions

lib/grape/middleware/error.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def default_options
2222
def call!(env)
2323
@env = env
2424

25+
inject_helpers!
26+
2527
begin
2628
error_response(catch(:error) do
2729
return @app.call(@env)
@@ -59,6 +61,19 @@ def exec_handler(e, &handler)
5961
end
6062
end
6163

64+
def inject_helpers!
65+
return if helpers_available?
66+
endpoint = @env['api.endpoint']
67+
self.class.instance_eval do
68+
include endpoint.send(:helpers)
69+
end if endpoint.is_a?(Grape::Endpoint)
70+
@helpers = true
71+
end
72+
73+
def helpers_available?
74+
@helpers
75+
end
76+
6277
def error!(message, status = options[:default_status], headers = {}, backtrace = [])
6378
headers = headers.reverse_merge(Grape::Http::Headers::CONTENT_TYPE => content_type)
6479
rack_response(format_message(message, backtrace), status, headers)

spec/grape/api_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,20 @@ def three
12921292
expect { get '/exception' }.to raise_error(RuntimeError, 'rain!')
12931293
end
12941294

1295+
it 'uses custom helpers defined by using #helpers method' do
1296+
subject.helpers do
1297+
def custom_error!(name)
1298+
error! "hello #{name}"
1299+
end
1300+
end
1301+
subject.rescue_from(ArgumentError) { custom_error! :bob }
1302+
subject.get '/custom_error' do
1303+
fail ArgumentError
1304+
end
1305+
get '/custom_error'
1306+
expect(last_response.body).to eq 'hello bob'
1307+
end
1308+
12951309
it 'rescues all errors if rescue_from :all is called' do
12961310
subject.rescue_from :all
12971311
subject.get '/exception' do

0 commit comments

Comments
 (0)