Skip to content

Commit

Permalink
Merge pull request #2 from affinity/scrivo/grape-exceptions-rescue-ha…
Browse files Browse the repository at this point in the history
…ndler

Allow specifying a handler for grape_exceptions
  • Loading branch information
mscrivo authored Jun 27, 2023
2 parents 1dce008 + f2015a3 commit c6a213e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/grape/dsl/request_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def rescue_from(*args, &block)
elsif args.include?(:grape_exceptions)
namespace_inheritable(:rescue_all, true)
namespace_inheritable(:rescue_grape_exceptions, true)
namespace_inheritable :grape_exceptions_rescue_handler, handler
else
handler_type =
case options[:rescue_subclasses]
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def build_stack(helpers)
rescue_options: namespace_stackable_with_hash(:rescue_options) || {},
rescue_handlers: namespace_reverse_stackable_with_hash(:rescue_handlers) || {},
base_only_rescue_handlers: namespace_stackable_with_hash(:base_only_rescue_handlers) || {},
all_rescue_handler: namespace_inheritable(:all_rescue_handler)
all_rescue_handler: namespace_inheritable(:all_rescue_handler),
grape_exceptions_rescue_handler: namespace_inheritable(:grape_exceptions_rescue_handler)

stack.concat namespace_stackable(:middleware)

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 @@ -109,7 +109,7 @@ def rescue_handler_for_grape_exception(klass)
return :error_response if klass == Grape::Exceptions::InvalidVersionHeader
return unless options[:rescue_grape_exceptions] || !options[:rescue_all]

:error_response
options[:grape_exceptions_rescue_handler] || :default_rescue_handler
end

def rescue_handler_for_any_class(klass)
Expand Down
24 changes: 22 additions & 2 deletions spec/grape/dsl/request_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,34 @@ def self.imbue(key, value)
it 'sets rescue all to true' do
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, nil)
subject.rescue_from :grape_exceptions
end

it 'sets rescue_grape_exceptions to true' do
it 'sets given proc as rescue handler' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
subject.rescue_from :grape_exceptions
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, rescue_handler_proc)
subject.rescue_from :grape_exceptions, rescue_handler_proc
end

it 'sets given block as rescue handler' do
rescue_handler_proc = proc {}
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, rescue_handler_proc)
subject.rescue_from :grape_exceptions, &rescue_handler_proc
end

it 'sets a rescue handler declared through :with option' do
with_block = -> { 'hello' }
expect(subject).to receive(:namespace_inheritable).with(:rescue_all, true)
expect(subject).to receive(:namespace_inheritable).with(:rescue_grape_exceptions, true)
expect(subject).to receive(:namespace_inheritable).with(:grape_exceptions_rescue_handler, an_instance_of(Proc))
subject.rescue_from :grape_exceptions, with: with_block
end

end

describe 'list of exceptions is passed' do
Expand Down

0 comments on commit c6a213e

Please sign in to comment.