feat!: start implementing repo listing & auth middleware
This commit is contained in:
39
api/src/error.rs
Normal file
39
api/src/error.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use actix_web::{HttpResponse, ResponseError, http::StatusCode};
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("db error: {0}")]
|
||||
DB(#[from] sqlx::Error),
|
||||
#[error("http error: {0}")]
|
||||
Http(#[from] reqwest::Error),
|
||||
#[error("error getting access token")]
|
||||
AccessToken,
|
||||
#[error("unauthorized")]
|
||||
Unauthorized,
|
||||
#[error("jwx error")]
|
||||
Jwx(#[from] jsonwebtoken::errors::Error),
|
||||
#[error("token expired")]
|
||||
TokenExpired,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErrorResponse {
|
||||
error: String,
|
||||
}
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
|
||||
match self {
|
||||
Error::AccessToken => HttpResponse::Unauthorized().finish(),
|
||||
Error::Unauthorized => HttpResponse::Unauthorized().finish(),
|
||||
Error::TokenExpired => HttpResponse::Unauthorized().json(ErrorResponse {
|
||||
error: "token expired".to_string(),
|
||||
}),
|
||||
_ => HttpResponse::InternalServerError().finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user