From d1fefb778dfafb8bbaf3fc6aa1c0ecc8a66df70e Mon Sep 17 00:00:00 2001 From: Jim Hodapp Date: Wed, 15 Nov 2023 18:09:16 -0600 Subject: [PATCH] Add EntityNotFound --> 404 error handling --- web/src/controller/organization_controller.rs | 8 ++++---- web/src/error.rs | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/web/src/controller/organization_controller.rs b/web/src/controller/organization_controller.rs index d5c8589..e73c70a 100644 --- a/web/src/controller/organization_controller.rs +++ b/web/src/controller/organization_controller.rs @@ -1,4 +1,4 @@ -use crate::AppState; +use crate::{AppState, Error}; use axum::extract::{Path, Query, State}; use axum::response::IntoResponse; use axum::Json; @@ -76,7 +76,7 @@ impl OrganizationController { State(app_state): State, Path(id): Path, Query(organization_params): Query, - ) -> impl IntoResponse { + ) -> Result, Error> { debug!( "UPDATE the entire Organization by id: {}, new name: {}", id, organization_params.name @@ -99,10 +99,10 @@ impl OrganizationController { .await .unwrap() } - None => organization::Model::default(), + None => return Err(Error::EntityNotFound), }; - Json(updated_organization) + Ok(Json(updated_organization)) } /// DELETE an Organization entity specified by its primary key diff --git a/web/src/error.rs b/web/src/error.rs index 7a6dca2..09a8436 100644 --- a/web/src/error.rs +++ b/web/src/error.rs @@ -5,12 +5,18 @@ pub type Result = core::result::Result; #[derive(Debug)] pub enum Error { - PlaceholderError, + InternalServer, + EntityNotFound, } impl IntoResponse for Error { fn into_response(self) -> Response { - (StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() + match self { + Error::InternalServer => { + (StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() + } + Error::EntityNotFound => (StatusCode::NOT_FOUND, "ENTITY NOT FOUND").into_response(), + } } }