taxy_api/
error.rs

1use serde_derive::{Deserialize, Serialize};
2use thiserror::Error;
3use utoipa::ToSchema;
4
5use crate::{id::ShortId, multiaddr::Multiaddr};
6
7#[derive(Serialize, Deserialize, ToSchema)]
8pub struct ErrorMessage {
9    pub message: String,
10    pub error: Option<Error>,
11}
12
13#[derive(Debug, Clone, Error, Serialize, Deserialize, ToSchema)]
14#[serde(rename_all = "snake_case", tag = "message")]
15#[non_exhaustive]
16pub enum Error {
17    #[error("invalid listening address: {addr}")]
18    InvalidListeningAddress {
19        #[schema(value_type = String)]
20        addr: Multiaddr,
21    },
22
23    #[error("invalid server address: {addr}")]
24    InvalidServerAddress {
25        #[schema(value_type = String)]
26        addr: Multiaddr,
27    },
28
29    #[error("invalid subject name: {name}")]
30    InvalidSubjectName { name: String },
31
32    #[error("invalid virtual host: {host}")]
33    InvalidVirtualHost { host: String },
34
35    #[error("invalid server url: {url}")]
36    InvalidServerUrl { url: String },
37
38    #[error("invalid multiaddr: {addr}")]
39    InvalidMultiaddr { addr: String },
40
41    #[error("missing TLS termination config")]
42    TlsTerminationConfigMissing,
43
44    #[error("failed to generate self-signed certificate")]
45    FailedToGenerateSelfSignedCertificate,
46
47    #[error("failed to read certificate")]
48    FailedToReadCertificate,
49
50    #[error("failed to read private key")]
51    FailedToReadPrivateKey,
52
53    #[error("invalid short id: {id}")]
54    InvalidShortId { id: String },
55
56    #[error("port id not found: {id}")]
57    IdNotFound { id: String },
58
59    #[error("port id already exists: {id}")]
60    IdAlreadyExists { id: ShortId },
61
62    #[error("acme account creation failed")]
63    AcmeAccountCreationFailed,
64
65    #[error("unauthorized")]
66    Unauthorized,
67
68    #[error("failed to create account")]
69    FailedToCreateAccount,
70
71    #[error("invalid login credentials")]
72    InvalidLoginCredentials,
73
74    #[error("too many login attempts")]
75    TooManyLoginAttempts,
76
77    #[error("failed to fetch log")]
78    FailedToFetchLog,
79
80    #[error("failed to invoke rpc")]
81    FailedToInvokeRpc,
82
83    #[error("failed to list network interfaces")]
84    FailedToListNetworkInterfaces,
85}
86
87impl Error {
88    pub fn status_code(&self) -> u16 {
89        match self {
90            Self::IdNotFound { .. } => 404,
91            Self::Unauthorized => 401,
92            Self::TooManyLoginAttempts => 429,
93            Self::FailedToFetchLog | Self::FailedToInvokeRpc => 500,
94            _ => 400,
95        }
96    }
97}