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

Allow specifying a handler for grape_exceptions #2342

Merged
merged 7 commits into from
Jul 3, 2023

Conversation

mscrivo
Copy link
Contributor

@mscrivo mscrivo commented Jun 27, 2023

This allows you to customize the format of the error response for grape exceptions:

For example, you could do something like this:

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'"
    }
  ]
}

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.

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'"
    }
  ]
}
```
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Looks interesting!

  1. Add API specs that exhibit the behavior described in the PR so we know what this is for.
  2. Fix tests ;)
  3. Update README and CHANGELOG.

@@ -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
Copy link
Member

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.

@mscrivo
Copy link
Contributor Author

mscrivo commented Jun 29, 2023

  1. Add API specs that exhibit the behavior described in the PR so we know what this is for.

Done

  1. Fix tests ;)

Done

  1. Update README and CHANGELOG.

Done

@mscrivo mscrivo requested a review from dblock June 29, 2023 14:07
@dblock
Copy link
Member

dblock commented Jun 29, 2023

Rubocop is unhappy, run rubocop -a ; rubocop --auto-gen-config and we're good to go!

@@ -276,6 +276,7 @@ def run
end
end

# rubocop:disable Metrics/MethodLength
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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!

Copy link
Contributor Author

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!

Copy link
Member

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

Screenshot 2023-07-04 at 12 42 55 PM

@mscrivo mscrivo requested a review from dblock July 3, 2023 14:11
@dblock dblock merged commit 96ac079 into ruby-grape:master Jul 3, 2023
23 checks passed
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