b7cd2b1
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use serde_json::json;
#[derive(Debug, Clone)]
pub enum SessionError {
Unauthorized,
Expired,
Verifier(String),
}
impl std::fmt::Display for SessionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unauthorized => write!(f, "unauthenticated"),
Self::Expired => write!(f, "token expired"),
Self::Verifier(message) => write!(f, "token rejected: {message}"),
}
}
}
impl std::error::Error for SessionError {}
impl SessionError {
pub fn code(&self) -> &'static str {
match self {
Self::Unauthorized => "unauthenticated",
Self::Expired => "token_expired",
Self::Verifier(_) => "invalid_token",
}
}
}
impl ResponseError for SessionError {
fn status_code(&self) -> StatusCode {
StatusCode::UNAUTHORIZED
}
fn error_response(&self) -> HttpResponse {
let message = self.code();
HttpResponse::Unauthorized().json(json!({
"code": 401,
"message": message,
"data": null,
"error": null,
"error_code": message,
"request_id": null,
"log_id": null,
}))
}
}