-
-
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
Passing status to the ErrorFormatter #2528
Passing status to the ErrorFormatter #2528
Conversation
e8f0495
to
cb87e68
Compare
…ible to ErrorFormatter children
cb87e68
to
d05f0ec
Compare
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 good, fix tests, likely needs documentation in https://github.com/ruby-grape/grape?tab=readme-ov-file#exception-handling and see if we can add lower level tests that a custom formatter for this.
Check failing tests for whether there's a non-backwards-compatible scenario, we don't want that.
lib/grape/error_formatter/base.rb
Outdated
@@ -4,7 +4,7 @@ module Grape | |||
module ErrorFormatter | |||
class Base | |||
class << self | |||
def call(message, backtrace, options = {}, env = nil, original_exception = nil) | |||
def call(message, backtrace, options = {}, env = nil, original_exception = nil, _status = nil) # rubocop:disable Metrics/ParameterLists |
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.
Use rubocop -a ; rubocop --auto-gen-config
instead of an inline #rubocop:disable
, we have a lot of violations.
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.
done, though I would probably just update the Metrics/ParameterLists
settings in .rubocop.yml
as 5 is a really small number limit parameter count.
Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
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.
Question below.
If the answer is yes, we need a backwards compatible implementation, maybe passing status
within options
?
@@ -2772,8 +2772,8 @@ Custom error formatters for existing and additional types can be defined with a | |||
|
|||
```ruby | |||
class Twitter::API < Grape::API | |||
error_formatter :txt, ->(message, backtrace, options, env, original_exception) { | |||
"error: #{message} from #{backtrace}" | |||
error_formatter :txt, ->(message, backtrace, options, env, original_exception, status) { |
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.
Is this a breaking change? Meaning if I had the old code and upgrade Grape, will this fail with an invalid number of parameters?
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.
Best I could tell this calls Grape::Middleware::Error.format_message
where I defaulted status to nil
. If someone created a custom Middleware there could be a problem. Fairpoint though.. I can check this with my own app and also look into making some more specs to verify.
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.
Since we document custom middleware this would be a breaking change :(
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 discuss @ericproulx's comment on this interface in #2527 (comment). If we are going to introduce a breaking change, I think we want something future-proof.
The other option is to add status
to the local scope, and not change this interface, which seems desirable.
@@ -2772,8 +2772,8 @@ Custom error formatters for existing and additional types can be defined with a | |||
|
|||
```ruby | |||
class Twitter::API < Grape::API | |||
error_formatter :txt, ->(message, backtrace, options, env, original_exception) { | |||
"error: #{message} from #{backtrace}" | |||
error_formatter :txt, ->(message, backtrace, options, env, original_exception, status) { |
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.
Since we document custom middleware this would be a breaking change :(
Closing this PR. With the fix in #2533 we can now access the HTTP status from the |
Thanks for hanging in here with us @drewnichols! I still wish we had a local scope for |
No worries, as this would be a breaking change it makes sense to do it in a major version update and put more thought into it to allow for future extensibility. |
The
status
paramter is not passed into theformat_message
method here inerror!
:grape/lib/grape/middleware/error.rb
Lines 132 to 137 in 5ce44de
Passing it allow it to be included in the response body JSON. This is the way it's included in the JSON API error response bodies: