|
1 | 1 | use std::{error, fmt}; |
2 | | - |
3 | | -/// The kind of error which occurred. |
4 | | -/// |
5 | | -/// Distinguishes between errors which were the result of user actions |
6 | | -/// and those which were the result of system failures. Conceptually |
7 | | -/// similar to HTTP status codes in that 4xx errors are user-caused |
8 | | -/// and 5xx errors are system-caused. |
9 | | -#[derive(Debug, PartialEq, Eq)] |
10 | | -pub enum Kind { |
11 | | - /// An error which was the result of actions that the user took. |
12 | | - /// |
13 | | - /// These errors are usually things which a user can easily resolve by |
14 | | - /// changing how they interact with the system. Advice should be used |
15 | | - /// to guide the user to the correct interaction paths and help them |
16 | | - /// self-mitigate without needing to open support tickets. |
17 | | - /// |
18 | | - /// These errors are usually generated with [`crate::user`], [`crate::user_with_cause`] |
19 | | - /// and [`crate::user_with_internal`]. |
20 | | - User, |
21 | | - |
22 | | - /// An error which was the result of the system failing rather than the user's actions. |
23 | | - /// |
24 | | - /// These kinds of issues are usually the result of the system entering |
25 | | - /// an unexpected state and/or violating an assumption on behalf of the |
26 | | - /// developer. Often these issues cannot be resolved by the user directly, |
27 | | - /// so the advice should guide them to the best way to raise a bug with you |
28 | | - /// and provide you with information to help them fix the issue. |
29 | | - /// |
30 | | - /// These errors are usually generated with [`crate::system`], [`crate::system_with_cause`] |
31 | | - /// and [`crate::system_with_internal`]. |
32 | | - System, |
33 | | -} |
34 | | - |
35 | | -impl Kind { |
36 | | - fn format_description(&self, description: &str) -> String { |
37 | | - match self { |
38 | | - Kind::User => format!("Oh no! {description}"), |
39 | | - Kind::System => format!("Whoops! {description} (This isn't your fault)"), |
40 | | - } |
41 | | - } |
42 | | -} |
| 2 | +use super::Kind; |
43 | 3 |
|
44 | 4 | /// The fundamental error type used by this library. |
45 | 5 | /// |
@@ -92,6 +52,27 @@ impl Error { |
92 | 52 | } |
93 | 53 | } |
94 | 54 |
|
| 55 | + /// Checks if this error is of a specific kind. |
| 56 | + /// |
| 57 | + /// Returns `true` if this error matches the provided [Kind], |
| 58 | + /// otherwise `false`. |
| 59 | + /// |
| 60 | + /// # Examples |
| 61 | + /// ``` |
| 62 | + /// use human_errors; |
| 63 | + /// |
| 64 | + /// let err = human_errors::user( |
| 65 | + /// "We could not open the config file you provided.", |
| 66 | + /// &["Make sure that the file exists and is readable by the application."], |
| 67 | + /// ); |
| 68 | + /// |
| 69 | + /// // Prints "is_user?: true" |
| 70 | + /// println!("is_user?: {}", err.is(human_errors::Kind::User)); |
| 71 | + /// ``` |
| 72 | + pub fn is(&self, kind: Kind) -> bool { |
| 73 | + self.kind == kind |
| 74 | + } |
| 75 | + |
95 | 76 | /// Gets the description message from this error. |
96 | 77 | /// |
97 | 78 | /// Gets the description which was provided as the first argument when constructing |
@@ -212,27 +193,6 @@ impl Error { |
212 | 193 |
|
213 | 194 | advice |
214 | 195 | } |
215 | | - |
216 | | - /// Checks if this error is of a specific kind. |
217 | | - /// |
218 | | - /// Returns `true` if this error matches the provided [Kind], |
219 | | - /// otherwise `false`. |
220 | | - /// |
221 | | - /// # Examples |
222 | | - /// ``` |
223 | | - /// use human_errors; |
224 | | - /// |
225 | | - /// let err = human_errors::user( |
226 | | - /// "We could not open the config file you provided.", |
227 | | - /// &["Make sure that the file exists and is readable by the application."], |
228 | | - /// ); |
229 | | - /// |
230 | | - /// // Prints "is_user?: true" |
231 | | - /// println!("is_user?: {}", err.is(human_errors::Kind::User)); |
232 | | - /// ``` |
233 | | - pub fn is(&self, kind: Kind) -> bool { |
234 | | - self.kind == kind |
235 | | - } |
236 | 196 | } |
237 | 197 |
|
238 | 198 | impl error::Error for Error { |
|
0 commit comments