-
-
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
error messages can now be presented #705
error messages can now be presented #705
Conversation
|
||
def present(message, env) | ||
if message.is_a?(Hash) | ||
message = message.dup.merge(code: env['api.endpoint'].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.
You don't want dup
here, merge
already copies the hash and returns another one.
2.0.0-p353 :001 > h = { foo: 'bar' }
=> {:foo=>"bar"}
2.0.0-p353 :002 > h.merge(bar: 'foo')
=> {:foo=>"bar", :bar=>"foo"}
2.0.0-p353 :003 > h
=> {:foo=>"bar"}
2.0.0-p353 :004 >
I think this is totally fine. |
@dblock Ok, added spec's and refactored it a little bit. |
I appreciate the efforts to clean up error presentation, it's definitely a weak point of the framework. Personally I'd prefer a more comprehensive approach as I outlined in #620 but unfortunately have not yet had the time to implement. I'm going to try to find some time this month to implement my proposal which I think covers this use case and more...do you agree that this PR would be unnecessary if #620 were implemented? |
The DSL cleanup is very nice. To have a class for descriptions is also very cool. But is this implementation compatible with grape-swagger? With your current implementation Errors are still not presentable with custom presenters. Don't force the user to use you'r In the long run you'r implementation is of course better (except the enforced error format). But done is better than perfect. |
@@ -944,6 +944,38 @@ instead of a message. | |||
error!({ error: "unexpected error", detail: "missing widget" }, 500) | |||
``` | |||
|
|||
When you'r error message is representable, a Hash with an option `with` or you'r |
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.
your
Let me sit on this PR for a bit and consider the proposals in #620. In the meantime, can you please make sure this build is passing clean? Thanks. |
I did make the changes. I can't make the build pass. This PR needs a new |
Really? Looking at the spec output I don't see why you couldn't make this backward compatible. What am I missing? |
|
I see. I'll do my best to release a grape-entity this week and will update/merge this. |
Thank you! What's about @mbleigh work? I realy like his cleaner way, but it need's a lot of work I think. |
I've released grape-entity, so this can be wrapped up. I wouldn't wait on @mbleigh's proposal because it's not close to being finished and this does move us forward, albeit not all the way. |
Nice. I did update dependencies and pushed new version. |
```ruby | ||
|
||
class API < Grape::API | ||
add_http_code_on_error true |
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 find this option either very confusing or bad practice.
It populates the result with an HTTP error code, but you already get the HTTP error code from ... HTTP. This creates an opportunity for a different value here vs. the HTTP response, for example if you have some middleware upstream that rewrites things (not uncommon).
I suggest removing it from this PR, and opening an issue for discussion. Generally there should be a way to augment errors with things like the HTTP status code or, say, a value of a header or something else without magic.
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.
Not every client can evaluate an HTTP client error. For instance when you have a batch script which uses your API with curl and a pipe then it is easier to evaluate the JSON data and grep the status code from there. Furthor more when your data contains an error code you know it was triggered by your server and not because a network error.
Or think of jsonp (http://www.theguardian.com/info/developer-blog/2012/jul/16/http-status-codes-jsonp). For JSONP you need an error in your data and can't use HTTP error codes.
With the middleware you named how do you ensure that you don't break the contract for your API? HTTP error codes are documented with swagger too.
So in my point of view it is valid to duplicate the HTTP error code, but if you want I can remove it.
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 want to think about it. Can you remove it for now and split it into a separate issue? Sorry for the hassle.
Ok, I did remove the error code presentation and fixed an issue when errors occour within a middleware. |
What's about the |
I rewrote the README part of this and committed squashed in 37362d8. Lets deal with whatever fails from here. |
I did open an ruby issue. require 'uri'
URI('//example.org/nested_optional_group?items[][key]=foo')
URI('//example.org/nested_optional_group?items[key]=foo')
URI('//example.org/nested_optional_group?items[]=foo') |
Yeah, I am not to worried about HEADs ;) |
So head should maybe removed from |
It's allowed to fail exactly for this kind of stuff. It's good that we catch Ruby bugs early and report them (like you did). |
I won't create a PR for adding status codes to error responses. I will solve this within my presenter. |
This PR is a proposal (and misses specs & co) to present error messages like started to discuss here.
It will be possible to do some thing like this:
This PR requires the upcomming version of
grape-entity
.