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

Router::layer should allow services whose Error: Into<Infallible> #922

Closed
lilyball opened this issue Apr 9, 2022 · 4 comments · Fixed by #924 or #948
Closed

Router::layer should allow services whose Error: Into<Infallible> #922

lilyball opened this issue Apr 9, 2022 · 4 comments · Fixed by #924 or #948
Labels
A-axum C-enhancement Category: A PR with an enhancement

Comments

@lilyball
Copy link

lilyball commented Apr 9, 2022

Bug Report

Version

axum v0.4.8
axum v0.5.0

Crates

axum

Description

I'm writing a generic tower::Service that needs to wrap two other services and thus combine their errors. I would like to represent this using a strongly-typed error enum, to be able to distinguish between errors raised by either service. The problem is if both services have Error = Infallible then my service will still have a strongly-typed uninhabited error. It can't ever produce an error but it won't be compatible with axum::Router::layer().

My idea here is to implement From<MyError<Infallible, Infallible>> for Infallible such that my error can be trivially converted to Infallible if it's uninhabited. This still won't make it directly compatible with Router::layer() though, but if Router::layer() required Error: Into<Infallible> rather than Error = Infallible it would all work.

More generally, any time that implements Into<Infallible> is presumably uninhabited, and axum really just cares that the error isn't possible rather than needing it to be the specific type Infallible. And so this should be reflected in the type system.

@lilyball
Copy link
Author

lilyball commented Apr 9, 2022

A potential worry here is if a layer supports multiple error types and is relying on the use-case to constrain it to a specific type, using Into<Infallible> will presumably cause an ambiguous type error. But such a layer would need to include this error type in its own type parameters and so the workaround is relatively simple.

Compare that with what we have now, where I have to wrap my layer in tower::ServiceBuilder::new().map_err(Into::into).layer(my_layer) just to map the error.

@davidpdrsn davidpdrsn added A-axum C-enhancement Category: A PR with an enhancement labels Apr 9, 2022
@davidpdrsn
Copy link
Member

Sounds fine to me!

davidpdrsn added a commit that referenced this issue Apr 19, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog
@davidpdrsn davidpdrsn reopened this Apr 19, 2022
@davidpdrsn
Copy link
Member

davidpdrsn commented Apr 19, 2022

Turns out #924 was a breaking change so we have to yank+revert. So keeping this issue open for now. This has unfortunately has to wait until axum 0.6 which we don't have a timeline for at the moment.

davidpdrsn added a commit that referenced this issue Apr 19, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog
@davidpdrsn
Copy link
Member

#948 is open now which includes this, however as mentioned its a breaking change so we cannot merge it yet.

However with the PR open I think I'll just close this for now as its accepted and implemented.

davidpdrsn added a commit that referenced this issue Jun 11, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 17, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 25, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 25, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 27, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 28, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 28, 2022
* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog
davidpdrsn added a commit that referenced this issue Jun 29, 2022
* Prepare axum-next branch

* Remove deprecated `extractor_middleware` function (#1077)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#948)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}` (#924)

* Allow `Error: Into<Infallible>` for `Route::{layer, route_layer}`

Fixes #922

* changelog

* fixup changelog

* Panic on overlapping routes in `MethodRouter` (#1102)

* Panic on overlapping routes in `MethodRouter`

* changelog link

* add test to ensure `head` and `get` don't overlap

* Fix changelog

* Prepare axum-next branch

* Remove trailing slash redirects

* changelog link

* Fix changelog

* remove asserting to make make the test more clear

* remove tsr related feature

* Add `RouterExt::route_with_tsr`

* Apply suggestions from code review

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* Update axum-extra/src/routing/mod.rs

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* fix typos in docs

* Update axum/CHANGELOG.md

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* mention `RouterExt::route_with_tsr` in the changelog

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum C-enhancement Category: A PR with an enhancement
Projects
None yet
2 participants