From 41d435c27e68c6d1f405d24fa058ae954ca663eb Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Sat, 19 Oct 2024 17:28:19 +0700 Subject: [PATCH] Improve error handling --- src/area/service.rs | 14 ++++++-------- src/element/v2.rs | 2 +- src/error.rs | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/area/service.rs b/src/area/service.rs index 2ab3d23..644711a 100644 --- a/src/area/service.rs +++ b/src/area/service.rs @@ -15,24 +15,24 @@ use time::OffsetDateTime; pub fn insert(tags: Map, conn: &Connection) -> Result { if !tags.contains_key("geo_json") { - return Err(Error::HttpBadRequest("geo_json tag is missing".into())); + return Err(Error::InvalidInput("geo_json tag is missing".into())); } let url_alias = tags .get("url_alias") - .ok_or(Error::HttpBadRequest( + .ok_or(Error::InvalidInput( "Mandatory tag is missing: url_alias".into(), ))? .as_str() - .ok_or(Error::HttpBadRequest( + .ok_or(Error::InvalidInput( "This tag should be a string: url_alias".into(), ))?; let geo_json = tags .get("geo_json") - .ok_or(Error::HttpBadRequest( + .ok_or(Error::InvalidInput( "Mandatory tag is missing: geo_json".into(), ))? .as_object() - .ok_or(Error::HttpBadRequest( + .ok_or(Error::InvalidInput( "This tag should be an object: geo_json".into(), ))?; let geo_json: Result = serde_json::to_string(geo_json).unwrap().parse(); @@ -77,9 +77,7 @@ pub fn patch_tags(id_or_alias: &str, tags: Map, conn: &Connection pub fn remove_tag(area_id_or_alias: &str, tag_name: &str, conn: &mut Connection) -> Result { if tag_name == "geo_json" { - return Err(Error::HttpBadRequest( - "geo_json tag can't be removed".into(), - )); + return Err(Error::InvalidInput("geo_json tag can't be removed".into())); } let area = Area::select_by_id_or_alias(area_id_or_alias, conn)?.unwrap(); Ok(Area::remove_tag(area.id, tag_name, conn)?.unwrap()) diff --git a/src/element/v2.rs b/src/element/v2.rs index 06f1d43..9b07f60 100644 --- a/src/element/v2.rs +++ b/src/element/v2.rs @@ -97,7 +97,7 @@ pub async fn get_by_id(id: Path, pool: Data>) -> Result() - .map_err(|_| Error::HttpBadRequest("Invalid ID".into()))?; + .map_err(|_| Error::InvalidInput("Invalid ID".into()))?; pool.get() .await? diff --git a/src/error.rs b/src/error.rs index 92198cb..ac4db23 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,7 +18,7 @@ pub enum Error { DeadpoolInteract(deadpool_sqlite::InteractError), DeadpoolConfig(deadpool_sqlite::ConfigError), DeadpoolBuild(deadpool_sqlite::BuildError), - HttpBadRequest(String), + InvalidInput(String), HttpUnauthorized(String), HttpConflict(String), Generic(String), @@ -41,7 +41,7 @@ impl Display for Error { Error::DeadpoolInteract(err) => err.fmt(f), Error::DeadpoolConfig(err) => err.fmt(f), Error::DeadpoolBuild(err) => err.fmt(f), - Error::HttpBadRequest(err) => write!(f, "{}", err), + Error::InvalidInput(err) => write!(f, "{}", err), Error::HttpConflict(err) => write!(f, "{}", err), Error::HttpUnauthorized(err) => write!(f, "{}", err), Error::Generic(err) => write!(f, "{}", err), @@ -136,7 +136,7 @@ impl From for Error { } pub fn query_error_handler(err: QueryPayloadError, _req: &HttpRequest) -> actix_web::Error { - Error::HttpBadRequest(format!("Invalid arguments: {err}")).into() + Error::InvalidInput(format!("Invalid arguments: {err}")).into() } #[derive(Serialize, Deserialize)] @@ -155,7 +155,7 @@ impl ResponseError for Error { fn status_code(&self) -> StatusCode { match self { - Error::HttpBadRequest(_) => StatusCode::BAD_REQUEST, + Error::InvalidInput(_) => StatusCode::BAD_REQUEST, Error::HttpUnauthorized(_) => StatusCode::UNAUTHORIZED, Error::NotFound(_) => StatusCode::NOT_FOUND, Error::HttpConflict(_) => StatusCode::CONFLICT,