Skip to content

Commit

Permalink
Update Failure comparison with Whatever
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed May 5, 2021
1 parent 18f0986 commit ea0a324
Showing 1 changed file with 7 additions and 68 deletions.
75 changes: 7 additions & 68 deletions src/guide/comparison/failure.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,30 @@
# SNAFU vs. Failure

This comparison was made against the examples in [the guide for
failure 0.1.5][failure-guide].
failure 0.1.8][failure-guide].

[failure-guide]: https://rust-lang-nursery.github.io/failure/guidance.html

## "Strings as errors"

It's unclear what benefit Failure provides here. If you are using this
functionality, we recommend using the standard library's `Box<dyn
Error>`:
If you wanted to do something similar with SNAFU, you can use the
[`Whatever`](crate::Whatever) type:

```rust
fn example() -> Result<(), Box<dyn std::error::Error>> {
Err(format!("Something went bad: {}", 1 + 1))?;
Ok(())
}
```

If you wanted to do something similar with SNAFU, you can create a
single-variant error enum with `String` data:

```rust
use snafu::Snafu;
use std::ops::Range;
use snafu::{Whatever, whatever};

#[derive(Debug, Snafu)]
enum Error {
Any { detail: String },
}

fn check_range(x: usize, range: Range<usize>) -> Result<usize, Error> {
fn check_range(x: usize, range: Range<usize>) -> Result<usize, Whatever> {
if x < range.start {
return AnySnafu {
detail: format!("{} is below {}", x, range.start),
}
.fail();
whatever!("{} is below {}", x, range.start);
}
if x >= range.end {
return AnySnafu {
detail: format!("{} is above {}", x, range.end),
}
.fail();
whatever!("{} is above {}", x, range.end);
}
Ok(x)
}
```

This could be enhanced in a few ways:

- create methods on your `Error` type
- create a custom macro
- add a [`Backtrace`][Backtrace] to the enum variant

For example:

```rust
use snafu::{Backtrace, Snafu};
use std::ops::Range;

#[derive(Debug, Snafu)]
enum Error {
Any {
detail: String,
backtrace: Backtrace,
},
}

macro_rules! format_err {
($($arg:tt)*) => { AnySnafu { detail: format!($($arg)*) }.fail() }
}

fn check_range(x: usize, range: Range<usize>) -> Result<usize, Error> {
if x < range.start {
return format_err!("{} is below {}", x, range.start);
}
if x >= range.end {
return format_err!("{} is above {}", x, range.end);
}
Ok(x)
}
```

Please see the next section for the recommended pattern for this error.

[Backtrace]: crate::Backtrace

## "A Custom Fail type" and "Using the Error type"

These two idioms from Failure are combined into one primary use case
Expand Down

0 comments on commit ea0a324

Please sign in to comment.