Select Git revision
errors.rs 3.10 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("Could not read SMS at path \"{0}\": {1}")]
ReadError(String, 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::ReadError(_s, _x) => "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::ReadError(..) => StatusCode::INTERNAL_SERVER_ERROR,
SmsError::UnknownEventKind(_) => StatusCode::INTERNAL_SERVER_ERROR,
SmsError::DeserializationError(_) => StatusCode::INTERNAL_SERVER_ERROR,
SmsError::NotInWhiteList(_) => StatusCode::FORBIDDEN,
SmsError::InBlackList(_) => StatusCode::FORBIDDEN,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorResponse {
pub code: u16,
pub error: String,
pub message: String,
}