tower icon indicating copy to clipboard operation
tower copied to clipboard

`ServiceExt::or_else`

Open Kestrer opened this issue 4 years ago • 2 comments

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.

Kestrer avatar Aug 03 '21 17:08 Kestrer

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.

davidpdrsn avatar Aug 03 '21 18:08 davidpdrsn

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.

Kestrer avatar Aug 04 '21 07:08 Kestrer