seastar
seastar copied to clipboard
Allow http exceptions to have content type
This PR allows httpd exceptions to optionally have a content type: if present the usual json encoding of the error reply is disabled and the body is used as is, and the response is set to the specified content type. Nothing changes for clients which don't set the content type.
This allows httpd to support non-json content when an error occurs, as it is already does for non-error replies. It also allows it to support json exceptions where the except message is already fully-formed json, to be emitted into the body as-is, rather than a simple string to be used in the to_json_exception format.
We have been using this patch for a few years in production w/o issue.
Copilot says:
Key updates include modifications to the base_exception class, updates to the generate_error_reply_and_close method, and adjustments to error handling logic in various components.
Enhancements to HTTP error handling:
Updates to base_exception:
- Added a new constructor to
base_exceptionthat accepts acontent_typeparameter, enabling the specification of a full response body format. - Introduced a
content_type()method inbase_exceptionto retrieve thecontent_typevalue.
Modifications to error reply generation:
- Updated the
generate_error_reply_and_closemethod inhttpd.hhandhttpd.ccto include acontent_typeparameter, which is used to set the content type of error replies. [1] [2] - Enhanced the
read_one()method inhttpd.ccto pass thecontent_typefrom exceptions to thegenerate_error_reply_and_closemethod.
Adjustments to exception handling in routes:
- Modified the
exception_replymethod inroutes.ccto check for the presence of acontent_typeinbase_exceptionand set the error reply accordingly. If nocontent_typeis provided, the error message is wrapped in a JSON response.