1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use multiaddr::Multiaddr;
use serde_derive::{Deserialize, Serialize};
use thiserror::Error;
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema)]
pub struct ErrorMessage {
    pub message: String,
    pub error: Option<Error>,
}

#[derive(Debug, Clone, Error, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case", tag = "message")]
pub enum Error {
    #[error("invalid listening address: {addr}")]
    InvalidListeningAddress {
        #[schema(value_type = [String])]
        addr: Multiaddr,
    },

    #[error("invalid server address: {addr}")]
    InvalidServerAddress {
        #[schema(value_type = [String])]
        addr: Multiaddr,
    },

    #[error("invalid subject name: {name}")]
    InvalidSubjectName { name: String },

    #[error("missing TLS termination config")]
    TlsTerminationConfigMissing,

    #[error("failed to generate self-signed certificate")]
    FailedToGerateSelfSignedCertificate,

    #[error("failed to read certificate")]
    FailedToReadCertificate,

    #[error("failed to read private key")]
    FailedToReadPrivateKey,

    #[error("certificate already exists: {id}")]
    CertAlreadyExists { id: String },

    #[error("certificate not found: {id}")]
    KeyringItemNotFound { id: String },

    #[error("port id not found: {id}")]
    IdNotFound { id: String },

    #[error("port id already exists: {id}")]
    IdAlreadyExists { id: String },

    #[error("acme account creation failed")]
    AcmeAccountCreationFailed,

    #[error("unauthorized")]
    Unauthorized,

    #[error("invalid login credentials")]
    InvalidLoginCredentials,

    #[error("failed to fetch log")]
    FailedToFetchLog,

    #[error("waiting log timed out")]
    WaitingLogTimedOut,

    #[error("rpc error")]
    RpcError,

    #[error("failed to load app key")]
    FailedToLoadAppKey,

    #[error("failed to encrypt private key")]
    FailedToEncryptPrivateKey,

    #[error("failed to decrypt private key")]
    FailedToDecryptPrivateKey,
}

#[cfg(feature = "warp")]
impl warp::reject::Reject for Error {}

impl Error {
    pub fn status_code(&self) -> u16 {
        match self {
            Self::KeyringItemNotFound { .. } | Self::IdNotFound { .. } => 404,
            Self::Unauthorized => 401,
            Self::WaitingLogTimedOut => 408,
            _ => 400,
        }
    }
}