Skip to main content

laravel_comments/
error.rs

1//! The package's errors.
2
3use std::error::Error;
4use std::fmt;
5
6use laravel::errors::Status;
7
8/// An operation on a comment that can't be done.
9#[derive(Debug, Clone, PartialEq, Eq, Status)]
10#[status(422)]
11#[non_exhaustive]
12pub enum CommentError {
13    /// The text is empty or only whitespace.
14    EmptyBody,
15    /// The text is longer than `COMMENTS_MAX_LENGTH`.
16    BodyTooLong {
17        /// The most characters allowed.
18        max: usize,
19    },
20    /// A guest's name or email address is missing.
21    InvalidGuest,
22    /// Replies are turned off: `COMMENTS_MAX_DEPTH` is `0`.
23    RepliesDisabled,
24    /// The comment was removed, so it can't get replies, edits, or reactions.
25    Deleted {
26        /// The comment's key.
27        id: i64,
28    },
29    /// The comment isn't approved, so it can't get replies or reactions.
30    NotApproved {
31        /// The comment's key.
32        id: i64,
33    },
34    /// The reaction isn't one of `COMMENTS_REACTIONS`.
35    UnknownReaction {
36        /// The reaction asked for.
37        reaction: String,
38    },
39    /// The comment is no longer in the table.
40    NotFound {
41        /// The comment's key.
42        id: i64,
43    },
44}
45
46impl fmt::Display for CommentError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Self::EmptyBody => f.write_str("A comment can't be empty."),
50            Self::BodyTooLong { max } => write!(f, "A comment can't be longer than {max} characters."),
51            Self::InvalidGuest => f.write_str("A guest's comment needs a name and an email address."),
52            Self::RepliesDisabled => f.write_str("Replies are turned off."),
53            Self::Deleted { id } => write!(f, "The comment `{id}` was removed."),
54            Self::NotApproved { id } => write!(f, "The comment `{id}` isn't approved."),
55            Self::UnknownReaction { reaction } => write!(f, "`{reaction}` isn't an allowed reaction."),
56            Self::NotFound { id } => write!(f, "The comment `{id}` no longer exists."),
57        }
58    }
59}
60
61impl Error for CommentError {}