-
Notifications
You must be signed in to change notification settings - Fork 20
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
http: add ability to pass response bodies in base_exception
#15
Conversation
Since an exception carries some text for the response body text, the raising site might like to specify the content type if it's e.g. json. Signed-off-by: John Spray <jcs@vectorized.io>
This enables throwing a base_exception from a json request handler with a json payload inside it. Signed-off-by: John Spray <jcs@vectorized.io>
Signed-off-by: John Spray <jcs@vectorized.io>
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.
lgtm. are we planning to submit this upstream?
@@ -82,7 +82,12 @@ std::unique_ptr<reply> routes::exception_reply(std::exception_ptr eptr) { | |||
} | |||
std::rethrow_exception(eptr); | |||
} catch (const base_exception& e) { | |||
rep->set_status(e.status(), json_exception(e).to_json()); | |||
if (e.content_type().size()) { | |||
rep->set_status(e.status(), e.str()); |
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.
why aren't both conditional cases the same w.r.t. set_status
(ie: json_exception(e).to_json()
)? I was thinking the only thing happening was that the content type was being plumbed through for context.
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.
json_exception
wraps the string up in a message like {"error": , "status": 400}
When content_type is set, the caller is indicating that they want to send the whole request body themselves (i.e. e.str() is the whole body, not a string to go within a json object)
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.
ahh got it thanks
We have a bit of an accumulation of smallish http changes that at some point should go upstream. |
In JSON request handlers, the string carried in the exception would be wrapped in a {"message": , "status":
} body. That prevented sending back a full response body to provide structured errors, e.g. dicts of request attributes to validation issues.
In this PR, a content_type field is added to base_exception, and if this content type is set then the exception's string is used as the full response body, and the response's content type header is set.