Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grape exception handling #1756

Merged
merged 2 commits into from
Apr 19, 2018
Merged

Grape exception handling #1756

merged 2 commits into from
Apr 19, 2018

Conversation

soylent
Copy link
Contributor

@soylent soylent commented Apr 18, 2018

This pull request fixes Grape exception handling for the following case:

class App < Grape::API
  rescue_from :grape_exceptions

  rescue_from Grape::Exceptions::ValidationErrors do |error|
    # BUG: This block is never called
    error!('redefined', 403)
  end

  params { requires :foo, type: String }

  get('/') { 'Hello' }
end

I reworked Grape::Middleware::Error a bit:

  • the code is easier to follow now
  • it works faster because we iterate over options[:rescue_handlers] and options[:base_only_rescue_handlers] only once
  • it allocates less garbage
  • fixed another covert bug: options[:base_only_rescue_handlers] should have higher priority than options[:rescue_handlers] because it is more restrictive

CHANGELOG.md Outdated
@@ -13,6 +13,7 @@
* [#1750](https://github.com/ruby-grape/grape/pull/1750): Fix a circular dependency warning due to router being loaded by API - [@salasrod](https://github.com/salasrod).
* [#1752](https://github.com/ruby-grape/grape/pull/1752): Fix `include_missing` behavior for aliased parameters - [@jonasoberschweiber](https://github.com/jonasoberschweiber).
* [#1754](https://github.com/ruby-grape/grape/pull/1754): Allow rescue from non-`StandardError` exceptions to use default error handling - [@jelkster](https://github.com/jelkster).
* [#1756](https://github.com/ruby-grape/grape/pull/1756): Allow custom Grape exception handlers when the build-in exception handling is enabled - [@soylent](https://github.com/soylent).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: builT-in

@@ -122,16 +85,51 @@ def format_message(message, backtrace, original_exception = nil)

private

def rescue_all?
options[:rescue_all]
DEFAULT_RESCUE_HANDLER = :handle_error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems pretty but maybe unnecessary? I mean :handle_error is already a constant, since this isn't going to change I say copy-paste it everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or maybe even rename it to :default_rescue_handler?

@dblock
Copy link
Member

dblock commented Apr 18, 2018

Perfect, thanks. See minor comments above.

Allow custom Grape exception handlers when the built-in exception
handling is enabled
@soylent
Copy link
Contributor Author

soylent commented Apr 19, 2018

@dblock All done

@dblock dblock merged commit 4782cea into ruby-grape:master Apr 19, 2018
@dblock
Copy link
Member

dblock commented Apr 19, 2018

Merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants