-
-
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
Should error! and error_response have the same method signature? #889
Comments
There's a lot of legacy around this, so most of the reasons for it to be this way are historical and not practical. Try making a PR that changes things the way you describe, I think it's definitely a worthy endeavor. |
@dblock I just got a chance to work on this and I was a bit confused. One big difference between the two method signatures is that When I run this example, I don't see a backtrace from /error. I do see one for /standard_error. It isn't clear to me if rack or grape is providing the backtrace for /standard_error. module Example
class API < Grape::API
format :json
get :standard_error do
raise StandardError.new("hello")
end
get :error do
raise Grape::Exceptions::Base
end
end
end From what I can tell, the formatter shouldn't be swallowing the backtrace: Is rack providing the backtrace or grape? Is is it the default behavior for formatters to swallow backtraces? In that case, it makes sense for the backtrace to be dropped as an argument. |
I think the backtrace of where the exception was raised is (should be) useful in both scenarios and the error formatter or whoever returns something should have access to it and be able to render it. |
I don't believe What I think you're looking for is https://github.com/intridea/grape/blob/master/lib/grape/middleware/error.rb line 70. You'll see the backtrace comes from the exception and then is passed to |
I just gave my first go at here. I tried my best to change as little as code as possible while matching the method signature of error! but I'm not sure how I should resolve the test cases that are currently failing. Most of the failures stem from the fact that handle_error returns a hash whose structure I'm respecting: def handle_error(e)
error_response(message: e.message, backtrace: e.backtrace)
end The tests expect that I return whatever's in the key :message but the point of the PR was to respect whatever was passed to error_response. Here's a sample of what I'm talking about: 1) Grape::API custom middleware .use mounts behind error middleware
Failure/Error: expect(last_response.body).to eq('Caught in the Net')
expected: "Caught in the Net"
got: "{\"message\":\"Caught in the Net\"}"
(compared using ==)
# ./spec/grape/api_spec.rb:981:in `block (4 levels) in <top (required)>' Any thoughts on how to proceed? I know it may not be clear what I'm saying, so feel free to ask me to elaborate. |
It's possible that you need to change the tests. Obviously you want to be able to render both a String ("Caught in the Net") and a JSON (message: ...), so as long as the tests cover both you're good. The important thing is that for those that used to just The next thing to think about is to explain in UPGRADING what users who called |
@dblock I'll give it some thought. I'll work on the tests this week and hopefully have something in mind for UPGRADING. |
Hey @dblock, I apologize for the delay on this. However, I believe I've found a better solution. I've created a method called My recommendation is to encourage the use of
People using If someone decides to use However, this does pose one problem: although I had updated all the tests, there was one that I couldn't get to pass. Would you mind taking a look? |
Here is the failing test: sunnyrjuneja@844b5c9 Here is the branch: https://github.com/whatasunnyday/grape/tree/error-new Once again, I'm sorry for the extreme delay on this PR. I hope adding this is still interesting to you. |
It is, would love an attempt at a fix? |
@dblock I gave it a shot. Any idea why it might be failing? Here's the stacktrace:
|
This json signature is incorrect. The method should be taking an argument as all these |
I was trying to use
error!
inside of arescue_from
block and got aNoMethodError
. I later founderror_response
and noticed that you don't get the same level of customization that you do fromerror!
.I'm proposing that
error_response
anderror!
have the same method signature. Their current implementations are as follows:My use case was to use
error_response
like I'm usingerror!
below:Instead, I'm skipping using
error_response
and returning aRack::Response
object like this:Apologies if I'm misunderstanding the API and there's a simple way to do the same thing.
The text was updated successfully, but these errors were encountered: