Files
Nishant Joshi d107b44fd3 feat(api_models): add error structs (#532)
Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com>
2023-02-16 09:19:14 +00:00

32 lines
1.3 KiB
Rust

use super::types::ApiErrorResponse;
impl actix_web::ResponseError for ApiErrorResponse {
fn status_code(&self) -> reqwest::StatusCode {
use reqwest::StatusCode;
match self {
Self::Unauthorized(_) => StatusCode::UNAUTHORIZED,
Self::ForbiddenCommonResource(_) => StatusCode::FORBIDDEN,
Self::ForbiddenPrivateResource(_) => StatusCode::NOT_FOUND,
Self::Conflict(_) => StatusCode::CONFLICT,
Self::Gone(_) => StatusCode::GONE,
Self::Unprocessable(_) => StatusCode::UNPROCESSABLE_ENTITY,
Self::InternalServerError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::NotImplemented(_) => StatusCode::NOT_IMPLEMENTED,
Self::ConnectorError(_, code) => *code,
Self::MethodNotAllowed(_) => StatusCode::METHOD_NOT_ALLOWED,
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
}
}
fn error_response(&self) -> actix_web::HttpResponse {
use actix_web::http::header;
actix_web::HttpResponseBuilder::new(self.status_code())
.insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
.insert_header((header::VIA, "Juspay_Router"))
.body(self.to_string())
}
}