Skip to content
Snippets Groups Projects
Select Git revision
  • 5165d3816f419db4fcf6e741d85a6acbfa34c37c
  • main default protected
2 results

errors.rs

Blame
  • errors.rs 2.87 KiB
    use thiserror::Error;
    use actix_web::{ResponseError, http::StatusCode, HttpResponse};
    use serde::{Serialize, Deserialize};
    
    #[allow(dead_code)]
    #[derive(Debug, Error, Serialize)]
    pub enum SmsError {
      #[error("SMS Text was to short")]
      TooShort,
      #[error("SMS Text was too long")]
      TooLong,
      #[error("The provided phonenumber was invalid: '{0}', Valid Example: +49157812345678")]
      InvaldNumber(String),
      #[error("The provided text used illegal characters. Use characters that are allowed by the GSM-7 spec.")]
      InvalidCharacters,
      #[error("Could not write the SMS to the outgoing directory: '{0}'")]
      WriteError(String),
      #[error("Received Event of unknown kind: '{0}'")]
      UnknownEventKind(String),
      #[error("Failed to deserialize SMS from string: '{0}'")]
      DeserializationError(String),
      #[error("The provided number '{0}' was not on the whitelist")]
      NotInWhiteList(String),
      #[error("The provided number '{0}' as it matched at least one entry of the blacklist")]
      InBlackList(String),
    }
    
    impl SmsError {
        pub fn name(&self) -> String {
            match self {
                SmsError::TooShort => "Bad Request".to_string(),
                SmsError::TooLong => "Bad Request".to_string(),
                SmsError::InvaldNumber(_s) => "Bad Request".to_string(),
                SmsError::InvalidCharacters => "Bad Request".to_string(),
                SmsError::WriteError(_s) => "Internal Server Error".to_string(),
                SmsError::UnknownEventKind(_s) => "Internal Server Error".to_string(),
                SmsError::DeserializationError(_s) => "Internal Server Error".to_string(),
                SmsError::NotInWhiteList(_) => "Forbidden".to_string(),
                SmsError::InBlackList(_) => "Forbidden".to_string(),
            }
        }
    }
    
    
    impl ResponseError for SmsError {
        fn error_response(&self) -> HttpResponse {
            let status_code = self.status_code();
            let error_response = ErrorResponse {
                code: status_code.as_u16(),
                message: self.to_string(),
                error: self.name(),
            };
            HttpResponse::build(status_code).json(error_response)
        }
    
        fn status_code(&self) -> StatusCode {
            match *self {
                SmsError::TooShort => StatusCode::BAD_REQUEST,
                SmsError::TooLong => StatusCode::BAD_REQUEST,
                SmsError::InvaldNumber(_) => StatusCode::BAD_REQUEST,
                SmsError::InvalidCharacters => StatusCode::BAD_REQUEST,
                SmsError::WriteError(_) => StatusCode::INTERNAL_SERVER_ERROR,
                SmsError::UnknownEventKind(_) => StatusCode::INTERNAL_SERVER_ERROR,
                SmsError::DeserializationError(_) => StatusCode::INTERNAL_SERVER_ERROR,
                SmsError::NotInWhiteList(_) => StatusCode::FORBIDDEN,
                SmsError::InBlackList(_) => StatusCode::FORBIDDEN,
            }
        }
    }