1use actix_web::{http::StatusCode, HttpResponse, ResponseError};
2use serde_json::json;
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum PAASServerError {
7 #[error("User not authenticated")]
8 NotAuthenticated,
9
10 #[error("Could not start, end or retrieve session")]
11 SessionError(#[source] Box<dyn std::error::Error + Send + Sync>),
12
13 #[error("Invalid session format: {0}")]
14 InvalidSessionFormat(String),
15
16 #[error("Unknown or expired session: {0}")]
17 InvalidSession(String),
18
19 #[error("Unauthorized session access")]
20 UnauthorizedSession,
21
22 #[error("Access denied: not allowed to transcrypt from {from} to {to}")]
23 AccessDenied { from: String, to: String },
24}
25
26impl ResponseError for PAASServerError {
27 fn status_code(&self) -> StatusCode {
28 match &self {
29 Self::NotAuthenticated => StatusCode::UNAUTHORIZED,
30 Self::SessionError(_) => StatusCode::INTERNAL_SERVER_ERROR,
31 Self::InvalidSessionFormat(_) => StatusCode::BAD_REQUEST,
32 Self::InvalidSession(_) => StatusCode::NOT_FOUND,
33 Self::UnauthorizedSession => StatusCode::FORBIDDEN,
34 Self::AccessDenied { .. } => StatusCode::FORBIDDEN,
35 }
36 }
37
38 fn error_response(&self) -> HttpResponse {
39 let status = self.status_code();
40 let error_message = self.to_string();
41
42 let response_body = if status == StatusCode::INTERNAL_SERVER_ERROR {
44 json!({
45 "error": "An internal server error occurred"
46 })
47 } else {
48 json!({
49 "error": error_message
50 })
51 };
52
53 HttpResponse::build(status)
54 .content_type("application/json")
55 .json(response_body)
56 }
57}