-
Notifications
You must be signed in to change notification settings - Fork 93
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
Handling errors outside of CRUD handlers #272
Comments
The middleware is running before our routing and resources. The But you can just use the |
Ok, cool, thanks for the info. I am doing the My thinking behind this is that I'm of the opinion that a JSON API should always return JSON objects, even in the case of error. The extreme example is that even a 404 error for a wrong API endpoint should return a JSON error object. A more common example (and the one I am dealing with) is handling a panic in an HTTP handler... I want to make sure we return a 500 with an error payload. The jsonapi.org spec is not clear on whether this is required or not, but I think it's a best practice. (I am going to ask on their repo for their opinion on this as well.) |
hm, yes I guess we can make that public, right @sharpner What I also noticed is that |
@joeshaw re auth, you can treat it like a sessions resource If there's no resource at the endpoint, then the response shouldn't have errors. I'm imaging a request and response that only have meta. Could you provide an example request and what the library is returning and what you'd like it to? |
The way I came to it was that I wrote middleware that caught panics and returns a 500 error. If you are calling a resource and it panics, you want a JSON response with an error response. This is what it ended up looking like:
Ideally, after constructing the |
I'm new to api2go and I'm really struggling to figure out how best to handle errors outside of the standard CRUD handling methods on resources.
For instance, if I have middleware, I'd like to be able to intercept requests but still return nicely formatted error JSON to clients. However, there doesn't appear to be any exported way to do that in the library.
I'd expect to do something like
api2go.NewHTTPError()
and then call a method to write that to thehttp.ResponseWriter
. There is amarshalHTTPError
function that would be nice to use for this, but it's private. There is also anapi.handleError()
method that could work, if it were exported (though I am not sure I necessarily have access to an*API
from inside my middleware)How do real-world middleware (for instance, ones that do auth) handle this? I couldn't find examples.
The text was updated successfully, but these errors were encountered: