Skip to main content

atomic_lti_tool/
errors.rs

1use actix_web::{HttpResponse, ResponseError};
2use atomic_lti::errors::{
3  AtomicError, DynamicRegistrationError, OIDCError, PlatformError, SecureError,
4};
5use serde::{Deserialize, Serialize};
6use serde_json::Error as JSONError;
7use thiserror::Error;
8
9#[derive(Error, Debug, PartialEq, Clone, Deserialize, Serialize)]
10pub enum AtomicToolError {
11  #[error("{0}")]
12  Internal(String),
13
14  #[error("Unauthorized request. {0}")]
15  Unauthorized(String),
16
17  #[error("Invalid request. {0}")]
18  InvalidRequest(String),
19
20  #[error("Missing permissions. {0}")]
21  InsufficientPermissions(String),
22}
23
24impl From<SecureError> for AtomicToolError {
25  fn from(err: SecureError) -> AtomicToolError {
26    AtomicToolError::Internal(err.to_string())
27  }
28}
29
30impl From<PlatformError> for AtomicToolError {
31  fn from(err: PlatformError) -> AtomicToolError {
32    AtomicToolError::Internal(err.to_string())
33  }
34}
35
36impl From<OIDCError> for AtomicToolError {
37  fn from(err: OIDCError) -> AtomicToolError {
38    AtomicToolError::Internal(err.to_string())
39  }
40}
41
42impl From<JSONError> for AtomicToolError {
43  fn from(err: JSONError) -> AtomicToolError {
44    AtomicToolError::Internal(err.to_string())
45  }
46}
47
48impl From<AtomicError> for AtomicToolError {
49  fn from(err: AtomicError) -> AtomicToolError {
50    AtomicToolError::Internal(err.to_string())
51  }
52}
53
54impl From<DynamicRegistrationError> for AtomicToolError {
55  fn from(err: DynamicRegistrationError) -> AtomicToolError {
56    AtomicToolError::InvalidRequest(err.to_string())
57  }
58}
59
60impl ResponseError for AtomicToolError {
61  fn error_response(&self) -> HttpResponse {
62    match self {
63      AtomicToolError::Internal(msg) => HttpResponse::InternalServerError().body(msg.to_owned()),
64      AtomicToolError::Unauthorized(msg) => HttpResponse::Unauthorized().body(msg.to_owned()),
65      AtomicToolError::InsufficientPermissions(msg) => {
66        HttpResponse::Unauthorized().body(msg.to_owned())
67      }
68      AtomicToolError::InvalidRequest(msg) => HttpResponse::BadRequest().body(msg.to_owned()),
69    }
70  }
71
72  fn status_code(&self) -> actix_web::http::StatusCode {
73    match self {
74      AtomicToolError::Internal(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
75      AtomicToolError::Unauthorized(_) => actix_web::http::StatusCode::UNAUTHORIZED,
76      AtomicToolError::InsufficientPermissions(_) => actix_web::http::StatusCode::UNAUTHORIZED,
77      AtomicToolError::InvalidRequest(_) => actix_web::http::StatusCode::BAD_REQUEST,
78    }
79  }
80}