yggdrasil/src/util/errors.rs

52 lines
1.8 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;
#[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()
})
}
}