91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
/*
|
|
* Yggdrasil: Minecraft authentication server
|
|
* Copyright (C) 2023 0xf8.dev@proton.me
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
use std::{error::Error, fmt};
|
|
|
|
use serde_json::json;
|
|
|
|
use crate::errors::YggdrasilErrorType::{BadRequestException, BaseYggdrasilException, ForbiddenOperationException, IllegalArgumentException};
|
|
|
|
#[derive(Debug)]
|
|
pub struct YggdrasilError(pub YggdrasilErrorType, pub String, pub u16, pub bool);
|
|
// error type, cause, status code, do pass through
|
|
|
|
#[derive(Debug)]
|
|
pub enum YggdrasilErrorType {
|
|
BaseYggdrasilException,
|
|
ForbiddenOperationException,
|
|
BadRequestException,
|
|
IllegalArgumentException,
|
|
}
|
|
|
|
impl Error for YggdrasilError {}
|
|
|
|
impl fmt::Display for YggdrasilError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
use YggdrasilErrorType::*;
|
|
|
|
match self.0 {
|
|
ForbiddenOperationException => write!(f, "FORBIDDEN"),
|
|
BadRequestException => write!(f, "BAD_REQUEST"),
|
|
_ => write!(f, "INTERNAL_SERVER_ERROR"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl YggdrasilError {
|
|
pub fn to_json(&self) -> serde_json::Value {
|
|
json!({
|
|
"errorType": format!("{}", self),
|
|
"error": format!("{:?}", self.0),
|
|
"errorMessage": self.1.to_owned(),
|
|
"developerMessage": self.1.to_owned()
|
|
})
|
|
}
|
|
|
|
pub fn new_base(msg: &str) -> Self {
|
|
Self {
|
|
0: BaseYggdrasilException,
|
|
1: msg.to_string(),
|
|
2: 500,
|
|
3: false,
|
|
}
|
|
}
|
|
|
|
pub fn new_forbidden(msg: &str) -> Self {
|
|
Self {
|
|
0: ForbiddenOperationException,
|
|
1: msg.to_string(),
|
|
2: 403,
|
|
3: false,
|
|
}
|
|
}
|
|
|
|
pub fn new_bad_request(msg: &str) -> Self {
|
|
Self {
|
|
0: BadRequestException,
|
|
1: msg.to_string(),
|
|
2: 400,
|
|
3: false,
|
|
}
|
|
}
|
|
|
|
pub fn new_illegal_argument(msg: &str) -> Self {
|
|
Self {
|
|
0: IllegalArgumentException,
|
|
1: msg.to_string(),
|
|
2: 500,
|
|
3: false
|
|
}
|
|
}
|
|
|
|
}
|