Skip to main content

utopia_domains/
error.rs

1//! The errors of domain parsing.
2
3/// Why an operation failed.
4///
5/// PHP: one variant per exception class the library raises.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7pub enum Error {
8    /// A URL given as a domain.
9    ///
10    /// PHP: `\Exception`.
11    #[error("{message}")]
12    Exception {
13        /// What went wrong.
14        message: String,
15        /// The code, `0` when none.
16        code: i64,
17    },
18    /// A Public Suffix List that could not be read or parsed.
19    #[error("{0}")]
20    Data(String),
21}
22
23impl Error {
24    /// The fully qualified name of the exception class this error stands for.
25    pub fn php_class(&self) -> &'static str {
26        match self {
27            Error::Exception { .. } => "Exception",
28            Error::Data(_) => "TypeError",
29        }
30    }
31
32    /// The numeric code: `0` for errors without one.
33    ///
34    /// PHP: `getCode()`.
35    pub fn code(&self) -> i64 {
36        match self {
37            Error::Exception { code, .. } => *code,
38            Error::Data(_) => 0,
39        }
40    }
41
42    pub(crate) fn exception(message: impl Into<String>, code: i64) -> Self {
43        Error::Exception { message: message.into(), code }
44    }
45}