tower
tower copied to clipboard
`ServiceExt::or_else`
ServiceExt::and_then already exists, so it makes sense to also have an or_else that goes from Self::Error -> Result<Self::Response, E>. Concrete use case: Axum currently has to redefine this itself in Layered::handle_error, but it would be nice to generalize it to Tower as whole.
Somewhat related: I tried porting axums HandleError to Tower but it has issues with backpressure https://github.com/tower-rs/tower/pull/593. I personally think the issues are fine for axum because of the limited use-case but probably doesn't work for Tower in general.
I think an Error: From<Self::Error> bound is fine, given it's an established pattern. Users can trivially do .map_err(|e| match e {}) (and with !, .map_err(|e| e)) to fix the Infallible problem. But if we add both handle_error and or_else it shouldn't even arise.