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

Allow customizing the server's default error response #3844

Open
stevenj opened this issue Jan 31, 2025 · 1 comment
Open

Allow customizing the server's default error response #3844

stevenj opened this issue Jan 31, 2025 · 1 comment
Labels
A-http1 Area: HTTP/1 specific. A-server Area: server. C-feature Category: feature. This is adding a new feature. E-medium Effort: medium. Some knowledge of how hyper internal works would be useful.

Comments

@stevenj
Copy link

stevenj commented Jan 31, 2025

Version
1.6.0

Platform
Linux x86-64

Description

When hyper gets a header with 0x7f in it, it generates a 400 response with no content type specified and no body.

The problem is, our OpenApi specification for our service defines a different response for 400, which matches the behavior of our services endpoints.

However, OpenAPI 3.0.x only allows you to define multiple response types for the same response code IF and ONLY IF they ALL have a content type. You can only have one response schema per distinct content type.

This breaks our CI. Our automated tests trigger hypers default 400 response. The response does not conform with our OpenAPI spec, so the breakage is 100% correct. It's not possible to define an OpenAPI spec which includes both hypers 400 response, and our designed 400 response.

The upshot is, Hyper has squatted on 400 and we can't re-define it, if we want a valid OpenAPI specification, and a service which 100% reflects it.
So our choices if we want to use 400 and have a valid spec is return no information in the error, this is not possible for us.
This has meant we can't use 400 at all.

Interestingly, hyper internally comments the response can be any 4xx, but the trait that would be needed to customize the Server struct is not public outside the crate, so it has proven impossible to get Hyper to have different behavior than an empty 400 with no content type.

We have spent a lot of time trying to solve this, but have we missed something and it is possible to redefine what hyper will return for 400?

@stevenj stevenj added the C-bug Category: bug. Something is wrong. This is bad! label Jan 31, 2025
@seanmonstar seanmonstar added A-server Area: server. C-feature Category: feature. This is adding a new feature. E-medium Effort: medium. Some knowledge of how hyper internal works would be useful. A-http1 Area: HTTP/1 specific. and removed C-bug Category: bug. Something is wrong. This is bad! labels Feb 6, 2025
@seanmonstar
Copy link
Member

I can see why someone would need to modify those automatic responses. I'd be fine with adding a mechanism to do so. Care would be required that it not leak too many internal details, so we can refactor things at any time and not worry about breaking this public API.

Would you be interested in exploring that with those constraints?

@seanmonstar seanmonstar changed the title Hypers default 400 response can not be changed and breaks OpenAPI 3.0.x schema tests. Allow customizing the server's default error response Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-http1 Area: HTTP/1 specific. A-server Area: server. C-feature Category: feature. This is adding a new feature. E-medium Effort: medium. Some knowledge of how hyper internal works would be useful.
Projects
None yet
Development

No branches or pull requests

2 participants