this repo has no description
at main 1.2 kB view raw
1use axum::http::StatusCode; 2use axum::response::{IntoResponse, Response}; 3use serde_json::json; 4 5#[derive(Debug, Clone)] 6pub enum ScopeError { 7 InsufficientScope { required: String, message: String }, 8 InvalidScope(String), 9} 10 11impl std::fmt::Display for ScopeError { 12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 13 match self { 14 ScopeError::InsufficientScope { message, .. } => write!(f, "{}", message), 15 ScopeError::InvalidScope(msg) => write!(f, "Invalid scope: {}", msg), 16 } 17 } 18} 19 20impl std::error::Error for ScopeError {} 21 22impl IntoResponse for ScopeError { 23 fn into_response(self) -> Response { 24 let (status, error_code, message) = match &self { 25 ScopeError::InsufficientScope { message, .. } => { 26 (StatusCode::FORBIDDEN, "InsufficientScope", message.clone()) 27 } 28 ScopeError::InvalidScope(msg) => (StatusCode::BAD_REQUEST, "InvalidScope", msg.clone()), 29 }; 30 ( 31 status, 32 axum::Json(json!({ 33 "error": error_code, 34 "message": message 35 })), 36 ) 37 .into_response() 38 } 39}