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
148
149
150
151
152
/*
 * error.rs
 *
 * deepwell-core - Database management and migrations service
 * Copyright (C) 2019-2020 Ammon Smith
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

use crate::UserId;
use diesel::result::{ConnectionError, Error as DieselError};
use std::io;
use subprocess::PopenError;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
    #[error("error: {0}")]
    StaticMsg(&'static str),

    #[error("general I/O error: {0}")]
    Io(#[from] io::Error),

    #[error("database error: {0}")]
    Database(#[from] DieselError),

    #[error("error connecting to database: {0}")]
    DatabaseConnection(#[from] ConnectionError),

    #[error("error running subprocess: {0}")]
    Subprocess(#[from] PopenError),

    #[error("command failed: {0}")]
    CommandFailed(String),

    #[error("invalid username or password")]
    AuthenticationFailed,

    #[error("session expired or invalid")]
    InvalidToken,

    #[error("not logged in")]
    NotLoggedIn,

    #[error("invalid password: {0}")]
    NewPasswordInvalid(&'static str),

    #[error("the given wiki was not found")]
    WikiNotFound,

    #[error("the given page was not found")]
    PageNotFound,

    #[error("the given page already exists")]
    PageExists,

    #[error("the page cannot be edited because a lock is present")]
    PageLocked(UserId),

    #[error("a page lock for the given user does not exist")]
    PageLockNotFound,

    #[error("the given user was not found")]
    UserNotFound,

    #[error("a user with the given name already exists")]
    UserNameExists,

    #[error("a user with the given email already exists")]
    UserEmailExists,

    #[error("the given revision was not found")]
    RevisionNotFound,

    #[error("the given revision does not correspond to the specified page")]
    RevisionPageMismatch,
}

impl Error {
    pub fn fixed_name(&self) -> &'static str {
        use self::Error::*;

        match *self {
            StaticMsg(_) => "custom",
            Io(_) => "io",
            Database(_) => "database",
            DatabaseConnection(_) => "database-connection",
            Subprocess(_) => "subprocess",
            CommandFailed(_) => "command-failed",
            AuthenticationFailed => "authentication-failed",
            InvalidToken => "invalid-token",
            NotLoggedIn => "not-logged-in",
            NewPasswordInvalid(_) => "invalid-password",
            WikiNotFound => "wiki-not-found",
            PageNotFound => "page-not-found",
            PageExists => "page-exists",
            PageLocked(_) => "page-locked",
            PageLockNotFound => "page-lock-not-found",
            UserNotFound => "user-not-found",
            UserNameExists => "user-name-exists",
            UserEmailExists => "user-email-exists",
            RevisionNotFound => "revision-not-found",
            RevisionPageMismatch => "revision-page-mismatch",
        }
    }

    #[inline]
    pub fn to_sendable(&self) -> SendableError {
        SendableError {
            name: self.fixed_name().into(),
            message: self.to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct SendableError {
    name: String,
    message: String,
}

impl SendableError {
    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn message(&self) -> &str {
        &self.message
    }
}

impl Into<(String, String)> for SendableError {
    #[inline]
    fn into(self) -> (String, String) {
        let Self { name, message } = self;

        (name, message)
    }
}