seastar icon indicating copy to clipboard operation
seastar copied to clipboard

Allow http exceptions to have content type

Open travisdowns opened this issue 4 months ago • 2 comments

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_exception that accepts a content_type parameter, enabling the specification of a full response body format.
  • Introduced a content_type() method in base_exception to retrieve the content_type value.

Modifications to error reply generation:

  • Updated the generate_error_reply_and_close method in httpd.hh and httpd.cc to include a content_type parameter, which is used to set the content type of error replies. [1] [2]
  • Enhanced the read_one() method in httpd.cc to pass the content_type from exceptions to the generate_error_reply_and_close method.

Adjustments to exception handling in routes:

  • Modified the exception_reply method in routes.cc to check for the presence of a content_type in base_exception and set the error reply accordingly. If no content_type is provided, the error message is wrapped in a JSON response.

travisdowns avatar Jun 09 '25 14:06 travisdowns