human_errors/
kind.rs

1/// The kind of error which occurred.
2///
3/// Distinguishes between errors which were the result of user actions
4/// and those which were the result of system failures. Conceptually
5/// similar to HTTP status codes in that 4xx errors are user-caused
6/// and 5xx errors are system-caused.
7#[derive(Debug, PartialEq, Eq)]
8pub enum Kind {
9    /// An error which was the result of actions that the user took.
10    ///
11    /// These errors are usually things which a user can easily resolve by
12    /// changing how they interact with the system. Advice should be used
13    /// to guide the user to the correct interaction paths and help them
14    /// self-mitigate without needing to open support tickets.
15    ///
16    /// These errors are usually generated with [`crate::user`], [`crate::user_with_cause`]
17    /// and [`crate::user_with_internal`].
18    User,
19
20    /// An error which was the result of the system failing rather than the user's actions.
21    ///
22    /// These kinds of issues are usually the result of the system entering
23    /// an unexpected state and/or violating an assumption on behalf of the
24    /// developer. Often these issues cannot be resolved by the user directly,
25    /// so the advice should guide them to the best way to raise a bug with you
26    /// and provide you with information to help them fix the issue.
27    ///
28    /// These errors are usually generated with [`crate::system`], [`crate::system_with_cause`]
29    /// and [`crate::system_with_internal`].
30    System,
31}
32
33impl Kind {
34    pub(crate) fn format_description(&self, description: &str) -> String {
35        match self {
36            Kind::User => format!("{description} (User error)"),
37            Kind::System => format!("{description} (System failure)"),
38        }
39    }
40}
41
42#[cfg(feature = "serde")]
43impl serde::Serialize for Kind {
44    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45    where
46        S: serde::Serializer,
47    {
48        match self {
49            Kind::User => serializer.serialize_str("User"),
50            Kind::System => serializer.serialize_str("System"),
51        }
52    }
53}