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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::permission::{ChannelPermission, HubPermission};
use parse_display::{Display, FromStr};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Errors related to data processing.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display, FromStr)]
#[display(style = "SNAKE_CASE")]
pub enum DataError {
    WriteFile,
    Deserialize,
    Directory,
    ReadFile,
    Serialize,
    DeleteFailed,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display, FromStr)]
#[display(style = "SNAKE_CASE")]
pub enum IndexError {
    OpenCreateIndex,
    CreateReader,
    CreateWriter,
    GetReader,
    GetWriter,
    ParseQuery,
    Search,
    GetDoc,
    Commit,
    Reload,
}

/// Errors related to authentication.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display, FromStr)]
#[display(style = "SNAKE_CASE")]
pub enum AuthError {
    NoResponse,
    BadJson,
    OAuthExchangeFailed,
    InvalidToken,
    InvalidSession,
    MalformedIDToken,
}

impl From<&AuthError> for StatusCode {
    fn from(error: &AuthError) -> Self {
        match error {
            AuthError::InvalidToken => Self::UNAUTHORIZED,
            AuthError::MalformedIDToken => Self::BAD_REQUEST,
            _ => StatusCode::BAD_GATEWAY,
        }
    }
}

/// General errors that can occur when using the WICRS API.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Display, FromStr)]
#[display(style = "SNAKE_CASE")]
pub enum Error {
    Muted,
    Banned,
    HubNotFound,
    ChannelNotFound,
    #[display("{}({0})")]
    MissingHubPermission(HubPermission),
    #[display("{}({0})")]
    MissingChannelPermission(ChannelPermission),
    NotInHub,
    UserNotFound,
    MemberNotFound,
    MessageNotFound,
    NotAuthenticated,
    GroupNotFound,
    InvalidName,
    UnexpectedServerArg,
    TooBig,
    InvalidMessage,
    MessageSendFailed,
    CannotAuthenticate,
    AlreadyTyping,
    NotTyping,
    InternalMessageFailed,
    Io,
    #[display("{}({0})")]
    Auth(AuthError),
    #[display("{}({0})")]
    Data(DataError),
    #[display("{}({0})")]
    Index(IndexError),
}

impl From<IndexError> for Error {
    fn from(err: IndexError) -> Self {
        Self::Index(err)
    }
}

impl From<AuthError> for Error {
    fn from(err: AuthError) -> Self {
        Self::Auth(err)
    }
}

impl From<DataError> for Error {
    fn from(err: DataError) -> Self {
        Self::Data(err)
    }
}

impl From<std::io::Error> for Error {
    fn from(_: std::io::Error) -> Self {
        Self::Io
    }
}

impl From<&Error> for StatusCode {
    fn from(error: &Error) -> Self {
        match error {
            Error::NotAuthenticated => Self::UNAUTHORIZED,
            Error::InvalidName => Self::BAD_REQUEST,
            Error::Banned => Self::FORBIDDEN,
            Error::ChannelNotFound => Self::NOT_FOUND,
            Error::GroupNotFound => Self::NOT_FOUND,
            Error::HubNotFound => Self::NOT_FOUND,
            Error::MemberNotFound => Self::NOT_FOUND,
            Error::MessageNotFound => Self::NOT_FOUND,
            Error::Muted => Self::FORBIDDEN,
            Error::MissingChannelPermission(_) => Self::FORBIDDEN,
            Error::MissingHubPermission(_) => Self::FORBIDDEN,
            Error::NotInHub => Self::NOT_FOUND,
            Error::UserNotFound => Self::NOT_FOUND,
            Error::UnexpectedServerArg => Self::INTERNAL_SERVER_ERROR,
            Error::TooBig => Self::BAD_REQUEST,
            Error::CannotAuthenticate => Self::INTERNAL_SERVER_ERROR,
            Error::InvalidMessage => Self::BAD_REQUEST,
            Error::MessageSendFailed => Self::INTERNAL_SERVER_ERROR,
            Error::AlreadyTyping => Self::CONFLICT,
            Error::NotTyping => Self::CONFLICT,
            Error::InternalMessageFailed => Self::INTERNAL_SERVER_ERROR,
            Error::Auth(error) => error.into(),
            Error::Data(_) => Self::INTERNAL_SERVER_ERROR,
            Error::Index(_) => Self::INTERNAL_SERVER_ERROR,
            Error::Io => Self::INTERNAL_SERVER_ERROR,
        }
    }
}