-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow specifying a handler for grape_exceptions #2342
Conversation
This allows you to customize the format of the error response for grape exceptions: For example, you could do something like this: ```rb rescue_from :grape_exceptions do |e| error!({ errors: [{ code: 'Error', message: e.message.squish }] }, e.status) end ``` which would render like this: ``` { "errors": [ { "code": "Error", "message": "Problem: message body does not match declared format Resolution: when specifying application/json as content-type, you must pass valid application/json in the request's 'body'" } ] } ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks interesting!
- Add API specs that exhibit the behavior described in the PR so we know what this is for.
- Fix tests ;)
- Update README and CHANGELOG.
lib/grape/dsl/request_response.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's match the format, or remove the (
above.
Done
Done
Done |
Rubocop is unhappy, run |
lib/grape/endpoint.rb
Outdated
@@ -276,6 +276,7 @@ def run | |||
end | |||
end | |||
|
|||
# rubocop:disable Metrics/MethodLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not do that and use rubocop -a ; rubocop --auto-gen-config
. The second one will fix it. We don't want to litter the code with rubocop ignores, mostly using it for auto-formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried that at first, but it produced all kinds of unrelated changes. The correct command is:
bundle exec rubocop -a ; bundle exec rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 5000
and really there should be a CI job that I can look at, that tells me that, but it seems you have to manually run CI checks on each commit? If you want more contributors, you should make this easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to ignore the unrelated changes, we just use rubocop for auto-formatting and minimal linting, so the rest is whatever.
GitHub doesn't let first time contributors auto-run CI on PRs for some security reason. I'll look into turning that off. FYI you can easily turn on GHA on your fork if you need it. Also, we do have 414 contributors, and sincerely appreciate your help!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub doesn't let first time contributors auto-run CI on PRs for some security reason. I'll look into turning that off.
That'd help a lot I think
FYI you can easily turn on GHA on your fork if you need it.
🤦🏼 I should have thought of that.
Anyhow, thanks for merging!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched it to the first option, but it doesn't let you disable this behavior. You can read about this more here: https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks
This allows you to customize the format of the error response for grape exceptions:
For example, you could do something like this:
which would render like this:
The main reason for wanting this change is to have grape-exceptions (which are very useful!) conform to the shape of other errors. I ran into this by creating an :all handler with the shape I wanted and realizing that I could not customize the shape of the grape exceptions.