arti/crates/retry-error/README.md

40 lines
1.1 KiB
Markdown
Raw Normal View History

2021-06-17 23:41:47 +01:00
# retry-error
2021-06-17 23:41:47 +01:00
An error attempt to represent multiple failures.
2021-06-17 23:41:47 +01:00
This crate implements [`RetryError`], a type to use when you
retry something a few times, and all those attempts can fail differently
each time. Instead of returning only a single error, it records
_all of the errors received_, in case they are different.
2021-06-17 23:41:47 +01:00
This crate is developed as part of
[Arti](https://gitlab.torproject.org/tpo/core/arti/), a project to
implement [Tor](https://www.torproject.org/) in Rust.
It's used by higher-level crates that retry
operations.
### Example
```rust
2021-06-17 23:41:47 +01:00
use retry_error::RetryError;
2021-11-29 14:15:15 +00:00
fn some_operation() -> anyhow::Result<bool> {
unimplemented!(); // example
2021-11-29 12:25:26 +00:00
}
2021-11-29 14:15:15 +00:00
fn example() -> Result<(), RetryError<anyhow::Error>> {
const N_ATTEMPTS: usize = 10;
let mut err = RetryError::in_attempt_to("perform an example operation");
for _ in 0..N_ATTEMPTS {
match some_operation() {
Ok(val) => return Ok(()),
Err(e) => err.push(e),
}
}
// All attempts failed; return all the errors.
return Err(err);
}
```
License: MIT OR Apache-2.0