Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 19, 2024
1 parent 073b42f commit c1b9c80
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub async fn get_by_url_alias(
.await?
.interact(move |conn| Area::select_by_alias(&cloned_url_alias, conn))
.await??;
area.ok_or(Error::HttpNotFound(format!(
area.ok_or(Error::NotFound(format!(
"Area with url_alias = {url_alias} doesn't exist"
)))
.map(|it| it.into())
Expand Down
4 changes: 1 addition & 3 deletions src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ pub async fn get_by_id(id: Path<String>, pool: Data<Arc<Pool>>) -> Result<Json<G
.await?
.interact(move |conn| Area::select_by_id_or_alias(&id_clone, conn))
.await??
.ok_or(Error::HttpNotFound(format!(
"Area with id {id} doesn't exist"
)))
.ok_or(Error::NotFound(format!("Area with id {id} doesn't exist")))
.map(|it| it.into())
}

Expand Down
2 changes: 1 addition & 1 deletion src/area_element/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.await?
.interact(move |conn| AreaElement::select_by_id(id_clone, conn))
.await??
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Area-element mapping with id {id} doesn't exist"
)))
.map(|it| it.into())
Expand Down
2 changes: 1 addition & 1 deletion src/element/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub async fn get_by_id(id: Path<String>, pool: Data<Arc<Pool>>) -> Result<Json<G
.interact(move |conn| Element::select_by_osm_type_and_id(&r#type, id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Element with id {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/element/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn get_by_id(id: Path<String>, pool: Data<Arc<Pool>>) -> Result<Json<G
.await?
.interact(move |conn| Element::select_by_id_or_osm_id(&id_clone, conn))
.await??
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Element with id {id} doesn't exist"
)))
.map(|it| it.into())
Expand Down
2 changes: 1 addition & 1 deletion src/element_comment/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.await?
.interact(move |conn| ElementComment::select_by_id(id_clone, conn))
.await??
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Element comment with id {id} doesn't exist"
)))
.map(|it| it.into())
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fmt::{Display, Formatter};

#[derive(Debug)]
pub enum Error {
NotFound(String),
IO(std::io::Error),
Rusqlite(rusqlite::Error),
Reqwest(reqwest::Error),
Expand All @@ -19,7 +20,6 @@ pub enum Error {
DeadpoolBuild(deadpool_sqlite::BuildError),
HttpBadRequest(String),
HttpUnauthorized(String),
HttpNotFound(String),
HttpConflict(String),
Generic(String),
Parse(time::error::Parse),
Expand All @@ -29,6 +29,7 @@ pub enum Error {
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::NotFound(err) => write!(f, "{}", err),
Error::IO(err) => err.fmt(f),
Error::Rusqlite(err) => err.fmt(f),
Error::Reqwest(err) => err.fmt(f),
Expand All @@ -41,7 +42,6 @@ impl Display for Error {
Error::DeadpoolConfig(err) => err.fmt(f),
Error::DeadpoolBuild(err) => err.fmt(f),
Error::HttpBadRequest(err) => write!(f, "{}", err),
Error::HttpNotFound(err) => write!(f, "{}", err),
Error::HttpConflict(err) => write!(f, "{}", err),
Error::HttpUnauthorized(err) => write!(f, "{}", err),
Error::Generic(err) => write!(f, "{}", err),
Expand Down Expand Up @@ -157,7 +157,7 @@ impl ResponseError for Error {
match self {
Error::HttpBadRequest(_) => StatusCode::BAD_REQUEST,
Error::HttpUnauthorized(_) => StatusCode::UNAUTHORIZED,
Error::HttpNotFound(_) => StatusCode::NOT_FOUND,
Error::NotFound(_) => StatusCode::NOT_FOUND,
Error::HttpConflict(_) => StatusCode::CONFLICT,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
Expand Down
2 changes: 1 addition & 1 deletion src/event/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| Event::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Event with id = {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/event/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| Event::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Event with id = {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/report/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| Report::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Report with id = {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/report/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| Report::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"Report with id = {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/user/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| User::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"User with id = {id} doesn't exist"
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/user/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn get_by_id(id: Path<i64>, pool: Data<Arc<Pool>>) -> Result<Json<GetI
.interact(move |conn| User::select_by_id(id, conn))
.await??
.map(|it| it.into())
.ok_or(Error::HttpNotFound(format!(
.ok_or(Error::NotFound(format!(
"User with id = {id} doesn't exist"
)))
}
Expand Down

0 comments on commit c1b9c80

Please sign in to comment.