Skip to content

Commit

Permalink
Document the whatever attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed May 3, 2021
1 parent 5f4ec89 commit 822ee4f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Snafu.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ unique situations.
- [`display`](#controlling-display)
- [`source`](#controlling-error-sources)
- [`visibility`](#controlling-visibility)
- [`whatever`](#controlling-stringly-typed-errors)

## Controlling `Display`

Expand Down Expand Up @@ -242,6 +243,32 @@ enum Error {
}
```

## Controlling stringly-typed errors

This allows your custom error type to behave like the [`Whatever`][]
error type. Since it is your type, you can implement additional
methods or traits. When placed on a struct or enum variant, you will
be able to use the type with the [`whatever!`][] macro as well as
`whatever_context` methods, such as [`ResultExt::whatever_context`][].

```rust
# use snafu::Snafu;
#[derive(Debug, Snafu)]
enum Error {
SpecificError { username: String },

#[snafu(whatever, display("{}", message))]
GenericError {
message: String,

// Having a `source` is optional, but if it is present, it must
// have this specific attribute and type:
#[snafu(source(from(Box<dyn std::error::Error>, Some)))]
source: Option<Box<dyn std::error::Error>>,
}
}
```

## Controlling how the `snafu` crate is resolved

If the `snafu` crate is not called `snafu` for some reason, you can
Expand Down

0 comments on commit 822ee4f

Please sign in to comment.