blob: e32c6d5979fe713144908778b8820191af154aca (
plain) (
tree)
|
|
use hex::FromHexError;
use ring::error::KeyRejected;
use thiserror::Error;
use axum::body;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use ring::error::Unspecified as RingUnspecified;
#[derive(Debug, Error)]
pub enum ServiceError {
#[error("Solarlib error: {0}")]
Solarlib(#[from] solarlib::errors::Error),
#[error("Axum error: {0}")]
Axum(#[from] axum::Error),
#[error("Not Found")]
NotFound,
#[error("Reqwest: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("Command error: {0}")]
Command(#[from] std::io::Error),
#[error("Not authorized")]
NotAuthorized,
#[error("Generic: {0}")]
Generic(String),
#[error("Invalid PASETO Key: {0}")]
PasetoInvalid(#[from] KeyRejected),
}
impl From<FromHexError> for ServiceError {
fn from(_: FromHexError) -> Self {
ServiceError::Generic(String::from("Could not convert from hex"))
}
}
impl From<RingUnspecified> for ServiceError {
fn from(_: RingUnspecified) -> Self {
ServiceError::Generic("Unspecified RNG error".to_string())
}
}
pub type StringResult<T = &'static str> = std::result::Result<T, ServiceError>;
pub type TokenResult<T = String> = std::result::Result<T, ServiceError>;
pub type JsonResult<T> = std::result::Result<T, ServiceError>;
pub type NoneResult = std::result::Result<(), ServiceError>;
impl IntoResponse for ServiceError {
fn into_response(self) -> Response {
let body = body::boxed(body::Full::from(self.to_string()));
let status = match self {
ServiceError::NotFound => StatusCode::NOT_FOUND,
ServiceError::NotAuthorized => StatusCode::UNAUTHORIZED,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
Response::builder().status(status).body(body).unwrap()
}
}
|