Skip to content
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

Merged
merged 3 commits into from
Dec 21, 2021

Conversation

jcsp
Copy link

@jcsp jcsp commented Dec 9, 2021

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.

jcsp added 3 commits December 9, 2021 14:10
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>
Copy link
Member

@dotnwat dotnwat left a 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());
Copy link
Member

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.

Copy link
Author

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh got it thanks

@jcsp
Copy link
Author

jcsp commented Dec 21, 2021

are we planning to submit this upstream?

We have a bit of an accumulation of smallish http changes that at some point should go upstream.

@jcsp jcsp merged commit f8ec733 into redpanda-data:master Dec 21, 2021
@jcsp jcsp deleted the http-exception-bodies branch December 21, 2021 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants