diff --git a/CHANGELOG.md b/CHANGELOG.md index 32bded2b0f..c796a59805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) =================== diff --git a/README.md b/README.md index 2ff6fa1c0c..7d51ff7269 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index 0180515fb2..88a7467107 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -22,6 +22,8 @@ def default_options def call!(env) @env = env + inject_helpers! + begin error_response(catch(:error) do return @app.call(@env) @@ -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) diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index ac1565e4e2..c553575f89 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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