ztp/src/errors.rs

25 lines
814 B
Rust

use axum::{http::StatusCode, response::IntoResponse, Json};
use serde_json::json;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ZTPError {
#[error("Form data was incomplete")]
FormIncomplete,
#[error("Email Address Already Subscribed")]
DuplicateEmail,
// #[error("Unknown error")]
// Unknown,
}
impl IntoResponse for ZTPError {
fn into_response(self) -> axum::response::Response {
let (status, error_message) = match self {
Self::FormIncomplete => (StatusCode::BAD_REQUEST, self.to_string()),
Self::DuplicateEmail => (StatusCode::BAD_REQUEST, self.to_string()),
// Self::Unknown => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
};
(status, Json(json!({ "error": error_message }))).into_response()
}
}