| 1 | # Using `#[derive(Error)]` |
| 2 | |
| 3 | Deriving `Error` will generate an `Error` implementation, that contains |
| 4 | (depending on the type) a `source()` and a `provide()` method. Please note, |
| 5 | at the time of writing `provide()` is only supported on nightly Rust. So you |
| 6 | have to use that to make use of it. |
| 7 | |
| 8 | For a struct, these methods always do the same. For an `enum` they have separate |
| 9 | behaviour for each of the variants. The variant is first matched and then the |
| 10 | implementation will do the same as it would have done if the variant was a |
| 11 | struct. |
| 12 | |
| 13 | Usually when you derive `Error` you will also want to [derive `Display`](crate::Display) and |
| 14 | often [`From` as well](crate::From). |
| 15 | |
| 16 | |
| 17 | ### When and how does it derive `source()`? |
| 18 | |
| 19 | 1. It's a struct/variant with named fields and one is the fields is |
| 20 | called `source`. Then it would return that field as the `source`. |
| 21 | 2. It's a tuple struct/variant and there's exactly one field that is not used as |
| 22 | the `backtrace`. So either a tuple struct with one field, or one with two where one |
| 23 | is the `backtrace`. Then it returns this field as the `source`. |
| 24 | 3. One of the fields is annotated with `#[error(source)]`. Then it would |
| 25 | return that field as the `source`. |
| 26 | |
| 27 | ### When and how does it derive `provide()`? |
| 28 | |
| 29 | 1. It's a struct/variant with named fields and one of the fields is |
| 30 | called `backtrace`. Then it would return that field as the `backtrace`. |
| 31 | 2. It's a tuple struct/variant and the type of exactly one of the fields is |
| 32 | called `Backtrace`. Then it would return that field as the `backtrace`. |
| 33 | 3. One of the fields is annotated with `#[error(backtrace)]`. Then it would |
| 34 | return that field as the `backtrace`. |
| 35 | |
| 36 | ### Ignoring fields for derives |
| 37 | |
| 38 | It's possible to ignore a field or a whole enum variant completely for this |
| 39 | derive using the `#[error(ignore)]` attribute. This will ignore it both for |
| 40 | detecting `backtrace` and `source`. It's also possible to mark a field only |
| 41 | ignored for one of these methods by using `#[error(not(backtrace))]` or |
| 42 | `#[error(not(source))]`. |
| 43 | |
| 44 | |
| 45 | ### What works in `no_std`? |
| 46 | |
| 47 | `Error` derive fully works on `no_std` environments, except the `provide()` |
| 48 | method usage, because the `Backtrace` type is only available in `std`. |
| 49 | |
| 50 | |
| 51 | ### `Option`al fields |
| 52 | |
| 53 | Deriving `source()` is supported naturally for `Option<_>`-typed fields. |
| 54 | |
| 55 | > **WARNING**: Field type is **considered as `Option<_>` only syntactically**, |
| 56 | > meaning that: |
| 57 | > - Neither renaming `std::option::Option` would work: |
| 58 | > ```rust,compile_fail |
| 59 | > # use derive_more::{Display, Error}; |
| 60 | > # |
| 61 | > #[derive(Debug, Display, Error)] |
| 62 | > struct Simple; |
| 63 | > |
| 64 | > #[derive(Debug, Display, Error)] |
| 65 | > #[display("Oops!")] |
| 66 | > struct Generic(RenamedOption<Simple>); // not recognized as `Option` |
| 67 | > type RenamedOption<T> = Option<T>; |
| 68 | > ``` |
| 69 | > - Nor using any custom type named as `Option` would: |
| 70 | > ```rust,compile_fail |
| 71 | > # use derive_more::{Display, Error}; |
| 72 | > # |
| 73 | > # #[derive(Debug, Display, Error)] |
| 74 | > # struct Simple; |
| 75 | > # |
| 76 | > #[derive(Debug, Display, Error)] |
| 77 | > #[display("No way!")] |
| 78 | > struct Generic(Option<Simple>); // cannot use `?` syntax |
| 79 | > struct Option<T>(T); |
| 80 | > ``` |
| 81 | |
| 82 | > **TIP**: If `std::option::Option` for some reason needs to be renamed, |
| 83 | > annotate the field with the `#[error(source(optional))]` attribute: |
| 84 | > ```rust |
| 85 | > # use derive_more::{Display, Error}; |
| 86 | > # |
| 87 | > #[derive(Debug, Display, Error)] |
| 88 | > struct Simple; |
| 89 | > |
| 90 | > #[derive(Debug, Display, Error)] |
| 91 | > #[display("Oops!")] |
| 92 | > struct Generic(#[error(source(optional))] RenamedOption<Simple>); |
| 93 | > type RenamedOption<T> = Option<T>; |
| 94 | > ``` |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | ## Example usage |
| 100 | |
| 101 | ```rust |
| 102 | # #![cfg_attr(nightly, feature(error_generic_member_access))] |
| 103 | # // Nightly requires enabling this feature: |
| 104 | # // #![feature(error_generic_member_access)] |
| 105 | # #[cfg(not(nightly))] fn main() {} |
| 106 | # #[cfg(nightly)] fn main() { |
| 107 | # use core::error::{request_ref, request_value, Error as __}; |
| 108 | # use std::backtrace::Backtrace; |
| 109 | # |
| 110 | # use derive_more::{Display, Error, From}; |
| 111 | # |
| 112 | # type RenamedOption<T> = Option<T>; |
| 113 | # |
| 114 | // `std::error::Error` requires `std::fmt::Debug` and `std::fmt::Display`, |
| 115 | // so we can also use `derive_more::Display` for fully declarative |
| 116 | // error-type definitions. |
| 117 | |
| 118 | #[derive(Default, Debug, Display, Error)] |
| 119 | struct Simple; |
| 120 | |
| 121 | #[derive(Default, Debug, Display, Error)] |
| 122 | struct WithSource { |
| 123 | source: Simple, |
| 124 | } |
| 125 | #[derive(Default, Debug, Display, Error)] |
| 126 | #[display("WithOptionalSource")] |
| 127 | struct WithOptionalSource { |
| 128 | source: Option<Simple>, |
| 129 | } |
| 130 | #[derive(Default, Debug, Display, Error)] |
| 131 | struct WithExplicitSource { |
| 132 | #[error(source)] |
| 133 | explicit_source: Simple, |
| 134 | } |
| 135 | #[derive(Default, Debug, Display, Error)] |
| 136 | #[display("WithExplicitOptionalSource")] |
| 137 | struct WithExplicitOptionalSource { |
| 138 | #[error(source)] |
| 139 | explicit_source: Option<Simple>, |
| 140 | } |
| 141 | #[derive(Default, Debug, Display, Error)] |
| 142 | #[display("WithExplicitOptionalSource")] |
| 143 | struct WithExplicitRenamedOptionalSource { |
| 144 | #[error(source(optional))] |
| 145 | explicit_source: RenamedOption<Simple>, |
| 146 | } |
| 147 | |
| 148 | #[derive(Default, Debug, Display, Error)] |
| 149 | struct Tuple(Simple); |
| 150 | |
| 151 | #[derive(Default, Debug, Display, Error)] |
| 152 | struct WithoutSource(#[error(not(source))] i32); |
| 153 | |
| 154 | #[derive(Debug, Display, Error)] |
| 155 | #[display("An error with a backtrace")] |
| 156 | struct WithSourceAndBacktrace { |
| 157 | source: Simple, |
| 158 | backtrace: Backtrace, |
| 159 | } |
| 160 | |
| 161 | // derive_more::From fits nicely into this pattern as well |
| 162 | #[derive(Debug, Display, Error, From)] |
| 163 | enum CompoundError { |
| 164 | Simple, |
| 165 | WithSource { |
| 166 | source: Simple, |
| 167 | }, |
| 168 | #[display("WithOptionalSource")] |
| 169 | WithOptionalSource { |
| 170 | source: Option<Simple>, |
| 171 | }, |
| 172 | #[from(ignore)] |
| 173 | WithBacktraceFromSource { |
| 174 | #[error(backtrace)] |
| 175 | source: Simple, |
| 176 | }, |
| 177 | #[display("{source}")] |
| 178 | WithDifferentBacktrace { |
| 179 | source: Simple, |
| 180 | backtrace: Backtrace, |
| 181 | }, |
| 182 | WithExplicitSource { |
| 183 | #[error(source)] |
| 184 | explicit_source: WithSource, |
| 185 | }, |
| 186 | #[display("WithExplicitOptionalSource")] |
| 187 | WithExplicitOptionalSource { |
| 188 | #[error(source)] |
| 189 | explicit_source: Option<WithSource>, |
| 190 | }, |
| 191 | #[display("WithExplicitRenamedOptionalSource")] |
| 192 | WithExplicitRenamedOptionalSource { |
| 193 | #[error(source(optional))] |
| 194 | explicit_source: RenamedOption<WithExplicitRenamedOptionalSource>, |
| 195 | }, |
| 196 | #[from(ignore)] |
| 197 | WithBacktraceFromExplicitSource { |
| 198 | #[error(backtrace, source)] |
| 199 | explicit_source: WithSource, |
| 200 | }, |
| 201 | Tuple(WithExplicitSource), |
| 202 | #[display("TupleOptional")] |
| 203 | TupleOptional(Option<WithExplicitSource>), |
| 204 | WithoutSource(#[error(not(source))] Tuple), |
| 205 | } |
| 206 | |
| 207 | assert!(Simple.source().is_none()); |
| 208 | assert!(request_ref::<Backtrace>(&Simple).is_none()); |
| 209 | |
| 210 | assert!(WithSource::default().source().is_some()); |
| 211 | assert!(WithOptionalSource { source: Some(Simple) }.source().is_some()); |
| 212 | |
| 213 | assert!(WithExplicitSource::default().source().is_some()); |
| 214 | assert!(WithExplicitOptionalSource { explicit_source: Some(Simple) }.source().is_some()); |
| 215 | assert!(WithExplicitRenamedOptionalSource { explicit_source: Some(Simple) }.source().is_some()); |
| 216 | |
| 217 | assert!(Tuple::default().source().is_some()); |
| 218 | |
| 219 | assert!(WithoutSource::default().source().is_none()); |
| 220 | |
| 221 | let with_source_and_backtrace = WithSourceAndBacktrace { |
| 222 | source: Simple, |
| 223 | backtrace: Backtrace::capture(), |
| 224 | }; |
| 225 | assert!(with_source_and_backtrace.source().is_some()); |
| 226 | assert!(request_ref::<Backtrace>(&with_source_and_backtrace).is_some()); |
| 227 | |
| 228 | assert!(CompoundError::Simple.source().is_none()); |
| 229 | |
| 230 | assert!(CompoundError::from(Simple).source().is_some()); |
| 231 | assert!(CompoundError::from(Some(Simple)).source().is_some()); |
| 232 | |
| 233 | assert!(CompoundError::from(WithSource::default()).source().is_some()); |
| 234 | assert!(CompoundError::from(Some(WithSource::default())).source().is_some()); |
| 235 | |
| 236 | assert!(CompoundError::from(WithExplicitSource::default()).source().is_some()); |
| 237 | assert!(CompoundError::from(Some(WithExplicitSource::default())).source().is_some()); |
| 238 | assert!(CompoundError::from(Some(WithExplicitRenamedOptionalSource { explicit_source: Some(Simple) })).source().is_some()); |
| 239 | |
| 240 | assert!(CompoundError::from(Tuple::default()).source().is_none()); |
| 241 | # } |
| 242 | ``` |