-
-
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
Was it intentional to hide the http response status from the error formatter? #2527
Comments
I don't think this was intentional at all, please feel free to PR a change. |
I will make it so |
Ain't the most intuitive way but you can access the status from the You can access endpoint's status code like this describe 'error formatter env' do
subject { last_response.body }
let(:app) do
Class.new(described_class) do
format :json
error_formatter :json, ->(message, _backtrace, _options, env, _original_exception) {
"#{message}:#{env[Grape::Env::API_ENDPOINT].status}"
}
rescue_from :all do
error!("I'm a teapot!", 418)
end
get { raise StandardError }
end
end
before do
get '/'
end
it { is_expected.to eq("I'm a teapot!:418") }
end @dblock To be more intuitive, I would suggest to revisit the signature to also include |
@drewnichols I think I agree with @ericproulx
|
The rescue_from NotAuthenticatedError do |e|
error!(e.message, 401, {}, e.backtrace, e)
end Ironically for grape errors rescued with the following rescue_from :grape_exceptions we get
I'm sure there is a fix for this but it feels like an indirect long-term solution that could be broken in the future. |
So any ideas about making this change backwards compatible? |
I need another week or so to work on it. Been busy with real job and it's snowing this weekend. |
I see the issue now. Anyone (like me) who created a formatter as a module like the following will get module CustomFormatter
def self.call(message, backtrace, options, env, original_exception)
{ message: message, backtrace: backtrace }
end
end I'm guessing this is not encouraged but we could get around this by inspecting the CustomFormatter if formatter.method(:call).arity == 6
return formatter.call(message, backtrace, options, env, original_exception, status)
else
return formatter.call(message, backtrace, options, env, original_exception)
end |
I dislike the arity check a lot, but I am not seeing another option either. My bigger concern is that this change is not future proof as we might find more things we want to pass along into custom formatters. Am I wrong to think this is a bad interface? We could step back a little. Some ideas.
WDYT? |
I think we should aim on a function with a signature that looks like this def call(**args)
...
end It gives a lot of flexibility and more future proof. Grape's internals can pass any parameters without breaking the signature. Plus, anyone can redefine with explicit keyword args. For instance def call(message:, status:, backtrace:, **_opts)
...
end |
I will check that. It supposed to return the appropriate status code |
@drewnichols I've a fix coming when using |
@drewnichols #2530 should fix the issue regarding the endpoint's status when using |
#2530 worked I can now access the HTTP status via |
I'm curious if this was discussed before and done intentionally.
For some reason, the
status
value is not passed into theformat_message
method here inerror!
:grape/lib/grape/middleware/error.rb
Lines 132 to 137 in 5ce44de
Passing it would make including it in the response body JSON easier. This is the way it's included in the JSON API error response bodies:
The text was updated successfully, but these errors were encountered: