Skip to content

Commit

Permalink
Add EntityNotFound --> 404 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jhodapp committed Nov 16, 2023
1 parent 4b5b553 commit d1fefb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 4 additions & 4 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::AppState;
use crate::{AppState, Error};
use axum::extract::{Path, Query, State};
use axum::response::IntoResponse;
use axum::Json;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl OrganizationController {
State(app_state): State<AppState>,
Path(id): Path<i32>,
Query(organization_params): Query<organization::Model>,
) -> impl IntoResponse {
) -> Result<Json<entity::organization::Model>, Error> {
debug!(
"UPDATE the entire Organization by id: {}, new name: {}",
id, organization_params.name
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ pub type Result<T> = core::result::Result<T, Error>;

#[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(),
}
}
}

Expand Down

0 comments on commit d1fefb7

Please sign in to comment.