at development 5.0 kB view raw
1//! Lumina > Server > Errors 2//! 3//! This module defines custom error types used throughout the server. 4 5/* 6 * Lumina/Peonies 7 * Copyright (C) 2018-2026 MLC 'Strawmelonjuice' Bloeiman and contributors. 8 * 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Affero General Public License as published 11 * by the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Affero General Public License for more details. 18 * 19 * You should have received a copy of the GNU Affero General Public License 20 * along with this program. If not, see <https://www.gnu.org/licenses/>. 21 */ 22 23#[derive(Debug)] 24pub(crate) enum LuminaError { 25 ConfInvalid(crate::EnvVar), 26 DbError(LuminaDbError), 27 Bb8RunErrorPg(bb8::RunError<crate::postgres::Error>), 28 Bb8RunErrorRedis(Box<bb8::RunError<redis::RedisError>>), 29 Unknown, 30 RocketFaillure(Box<rocket::Error>), 31 BcryptError, 32 RegisterEmailInUse, 33 RegisterUsernameInUse, 34 RegisterEmailNotValid, 35 RegisterUsernameInvalid(crate::user::OnRegisterUsernameInvalid), 36 RegisterPasswordNotValid(crate::user::OnRegisterPasswordNotValid), 37 AuthenticationWrongPassword, 38 UUidError, 39 RegexError, 40 SerializationError(serde_json::Error), 41 JoinFaillure, 42} 43 44impl From<LuminaDbError> for LuminaError { 45 fn from(v: LuminaDbError) -> Self { 46 Self::DbError(v) 47 } 48} 49 50#[derive(Debug)] 51pub(crate) enum LuminaDbError { 52 Redis(Box<redis::RedisError>), 53 Postgres(crate::postgres::Error), 54} 55 56impl From<rocket::Error> for LuminaError { 57 fn from(err: rocket::Error) -> Self { 58 LuminaError::RocketFaillure(Box::new(err)) 59 } 60} 61 62impl From<serde_json::Error> for LuminaError { 63 fn from(err: serde_json::Error) -> Self { 64 LuminaError::SerializationError(err) 65 } 66} 67 68impl From<crate::postgres::Error> for LuminaError { 69 fn from(err: crate::postgres::Error) -> Self { 70 LuminaError::DbError(LuminaDbError::Postgres(err)) 71 } 72} 73 74impl From<redis::RedisError> for LuminaError { 75 fn from(err: redis::RedisError) -> Self { 76 LuminaError::DbError(LuminaDbError::Redis(Box::new(err))) 77 } 78} 79impl From<bb8::RunError<crate::postgres::Error>> for LuminaError { 80 fn from(err: bb8::RunError<crate::postgres::Error>) -> Self { 81 LuminaError::Bb8RunErrorPg(err) 82 } 83} 84impl From<bb8::RunError<redis::RedisError>> for LuminaError { 85 fn from(err: bb8::RunError<redis::RedisError>) -> Self { 86 LuminaError::Bb8RunErrorRedis(Box::new(err)) 87 } 88} 89 90impl std::fmt::Display for LuminaError { 91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 92 write!( 93 f, 94 "{}", 95 match self { 96 LuminaError::ConfInvalid(s) => match s { 97 crate::EnvVar::LUMINA_SERVER_ADDR => 98 "LUMINA_SERVER_ADDR is an invalid address".to_string(), 99 crate::EnvVar::LUMINA_SERVER_PORT => 100 "LUMINA_SERVER_PORT is not a valid port number".to_string(), 101 crate::EnvVar::LUMINA_POSTGRES_PORT => 102 "LUMINA_POSTGRES_PORT is not a valid port number".to_string(), 103 }, 104 105 LuminaError::DbError(e) => match e { 106 LuminaDbError::Redis(re) => format!("Redis error: {}", re), 107 LuminaDbError::Postgres(pe) => format!("Postgres error: {}", pe), 108 }, 109 LuminaError::Bb8RunErrorPg(e) => format!("Postgres connection pool error: {}", e), 110 LuminaError::Bb8RunErrorRedis(e) => format!("Redis connection pool error: {}", e), 111 LuminaError::RocketFaillure(e) => format!("Rocket error: {}", e), 112 LuminaError::BcryptError => "Bcrypt error".to_string(), 113 LuminaError::RegisterEmailInUse => "Email already in use".to_string(), 114 LuminaError::RegisterUsernameInUse => "Username already in use".to_string(), 115 LuminaError::RegisterEmailNotValid => "Email not valid".to_string(), 116 LuminaError::RegisterUsernameInvalid(s) => format!("Username invalid: {}", s), 117 LuminaError::RegisterPasswordNotValid(s) => format!("Password not valid: {}", s), 118 LuminaError::AuthenticationWrongPassword => "Wrong password".to_string(), 119 LuminaError::UUidError => "UUID error".to_string(), 120 LuminaError::RegexError => "Regex error".to_string(), 121 LuminaError::SerializationError(s) => format!("Serialization error: {}", s), 122 LuminaError::JoinFaillure => "Process join failure".to_string(), 123 LuminaError::Unknown => "Unknown error".to_string(), 124 } 125 ) 126 } 127}