tower icon indicating copy to clipboard operation
tower copied to clipboard

ServiceExt::short_circuit

Open BrynCooke opened this issue 2 years ago • 1 comments

Currently the filter layer allows processing to be halted, but an error must be returned because Result is used to indicate continued processing. It'd be good to be able to have the same functionality but use a dedicated enum to indicate continued processing.

Example uses:

  • Caching - Return a response immediately (either Ok or Err)
  • Validation - Abort processing, but OK response object should be returned which contains error code and message. BoxError is not suitable because structured errors are lost.

Something like:

builder.short_circuit(|request| {
    ShortCircuit::Continue(request) //Continue processing using the supplied request
    ShortCircuit::Abort(Ok(response)) //Abort processing returning OK
    ShortCircuit::Abort(Err(error)) //Abort processing returning Err
});

Like filter, it would be good to have an async version. I'd be willing to create a PR for this.

BrynCooke avatar Mar 01 '22 14:03 BrynCooke

How about using https://doc.rust-lang.org/stable/std/ops/enum.ControlFlow.html ?

LegNeato avatar Mar 09 '22 08:03 LegNeato