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

HeaderMapExt should provide a typed_{try_}get alternative that handles missing headers #133

Open
RReverser opened this issue Feb 25, 2023 · 3 comments

Comments

@RReverser
Copy link

See the discussion in tokio-rs/axum#1781 (comment) for the original issue & motivation.

TL;DR is that Header::decode allows each typed header to handle empty iterators in its own way and let the header implementation decide whether missing header is an error or whether to provide some default "parsed" value (e.g. empty collection), but HeaderMapExt::typed_{try_}get always return Option::None when header is not found in the HeaderMap, not giving such Header implementation a chance to correctly provide their own fallback.

It would be useful to have variation of those methods that always return Result<H, Error> by forwarding an empty iterator to the specific H::decode implementation and letting it decide whether to provide a Ok(fallback_value) or emit an error.

@seanmonstar
Copy link
Member

Ah, interesting. I guess a question then to answer is, are these two things worth distinguishing: the header was never sent, versus it was sent with an empty value.

@RReverser
Copy link
Author

RReverser commented Feb 28, 2023

are these two things worth distinguishing: the header was never sent, versus it was sent with an empty value

I'd say it's really up to specific header implementation and/or consumer. I guess if you want to keep typed_try_get around alongside the new method, then consumers who want to check its existence, will be still able to do so, while others would probably not care and prefer to just get whatever spec dictates via the new method.

Alternatively, it could be useful to make Error an enum and then have a single method with Result<H, Error> signature. Then consumer will always easily get a header value where it's valid to do so, but will be able to distinguish between "header didn't exist" and "header was invalid" by looking at the error component if they want more granularity.

@RReverser
Copy link
Author

Alternatively, it could be useful to make Error an enum and then have a single method with Result<H, Error> signature. Then consumer will always easily get a header value where it's valid to do so, but will be able to distinguish between "header didn't exist" and "header was invalid" by looking at the error component if they want more granularity.

No, wait, I'm too tired by now, that doesn't help / doesn't make any sense.

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

No branches or pull requests

2 participants