110 lines
4.5 KiB
Rust
110 lines
4.5 KiB
Rust
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use actix_web::http::StatusCode;
|
|
use actix_web::{HttpResponse, ResponseError};
|
|
use derive_more::Display;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::inventory::application::services::errors::*;
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
|
|
struct ErrorResponse {
|
|
error: String,
|
|
}
|
|
|
|
impl From<WebError> for ErrorResponse {
|
|
fn from(value: WebError) -> Self {
|
|
ErrorResponse {
|
|
error: serde_json::to_string(&value).unwrap_or_else(|_| {
|
|
log::error!("Unable to serialize error");
|
|
"Unable to serialize error".into()
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub enum WebError {
|
|
InternalError,
|
|
BadRequest,
|
|
|
|
DuplicateCategoryName,
|
|
DuplicateStoreName,
|
|
DuplicateProductName,
|
|
DuplicateCustomizationName,
|
|
DuplicateCustomizationID,
|
|
DuplicateStoreID,
|
|
DuplicateCategoryID,
|
|
DuplicateProductID,
|
|
ProductIDNotFound,
|
|
CategoryIDNotFound,
|
|
CustomizationIDNotFound,
|
|
StoreIDNotFound,
|
|
}
|
|
|
|
impl From<InventoryError> for WebError {
|
|
fn from(v: InventoryError) -> Self {
|
|
match v {
|
|
InventoryError::InternalError => Self::InternalError,
|
|
InventoryError::DuplicateCategoryName => Self::BadRequest,
|
|
InventoryError::DuplicateStoreName => Self::BadRequest,
|
|
InventoryError::DuplicateProductName => Self::BadRequest,
|
|
InventoryError::DuplicateCustomizationName => Self::BadRequest,
|
|
InventoryError::DuplicateCustomizationID => Self::InternalError,
|
|
InventoryError::DuplicateStoreID => Self::InternalError,
|
|
InventoryError::DuplicateCategoryID => Self::InternalError,
|
|
InventoryError::DuplicateProductID => Self::InternalError,
|
|
InventoryError::ProductIDNotFound => Self::BadRequest,
|
|
InventoryError::CategoryIDNotFound => Self::BadRequest,
|
|
InventoryError::CustomizationIDNotFound => Self::BadRequest,
|
|
InventoryError::StoreIDNotFound => Self::BadRequest,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ResponseError for WebError {
|
|
fn status_code(&self) -> StatusCode {
|
|
match self {
|
|
Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::BadRequest => StatusCode::BAD_REQUEST,
|
|
|
|
Self::DuplicateCategoryName => StatusCode::BAD_REQUEST,
|
|
Self::DuplicateStoreName => StatusCode::BAD_REQUEST,
|
|
Self::DuplicateProductName => StatusCode::BAD_REQUEST,
|
|
Self::DuplicateCustomizationName => StatusCode::BAD_REQUEST,
|
|
Self::DuplicateCustomizationID => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::DuplicateStoreID => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::DuplicateCategoryID => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::DuplicateProductID => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Self::ProductIDNotFound => StatusCode::BAD_REQUEST,
|
|
Self::CategoryIDNotFound => StatusCode::BAD_REQUEST,
|
|
Self::CustomizationIDNotFound => StatusCode::BAD_REQUEST,
|
|
Self::StoreIDNotFound => StatusCode::BAD_REQUEST,
|
|
}
|
|
}
|
|
|
|
fn error_response(&self) -> actix_web::HttpResponse {
|
|
let e: ErrorResponse = self.clone().into();
|
|
match self {
|
|
Self::InternalError => HttpResponse::InternalServerError().json(e),
|
|
Self::BadRequest => HttpResponse::BadRequest().json(e),
|
|
|
|
Self::DuplicateCategoryName => HttpResponse::BadRequest().json(e),
|
|
Self::DuplicateStoreName => HttpResponse::BadRequest().json(e),
|
|
Self::DuplicateProductName => HttpResponse::BadRequest().json(e),
|
|
Self::DuplicateCustomizationName => HttpResponse::BadRequest().json(e),
|
|
Self::DuplicateCustomizationID => HttpResponse::InternalServerError().json(e),
|
|
Self::DuplicateStoreID => HttpResponse::InternalServerError().json(e),
|
|
Self::DuplicateCategoryID => HttpResponse::InternalServerError().json(e),
|
|
Self::DuplicateProductID => HttpResponse::InternalServerError().json(e),
|
|
Self::ProductIDNotFound => HttpResponse::BadRequest().json(e),
|
|
Self::CategoryIDNotFound => HttpResponse::BadRequest().json(e),
|
|
Self::CustomizationIDNotFound => HttpResponse::BadRequest().json(e),
|
|
Self::StoreIDNotFound => HttpResponse::BadRequest().json(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub type WebJsonRepsonse<V> = Result<V, WebError>;
|