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 c1b9c80 commit 41d435c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/area/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ use time::OffsetDateTime;

pub fn insert(tags: Map<String, Value>, conn: &Connection) -> Result<Area> {
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<GeoJson, _> = serde_json::to_string(geo_json).unwrap().parse();
Expand Down Expand Up @@ -77,9 +77,7 @@ pub fn patch_tags(id_or_alias: &str, tags: Map<String, Value>, conn: &Connection

pub fn remove_tag(area_id_or_alias: &str, tag_name: &str, conn: &mut Connection) -> Result<Area> {
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())
Expand Down
2 changes: 1 addition & 1 deletion src/element/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub async fn get_by_id(id: Path<String>, pool: Data<Arc<Pool>>) -> Result<Json<G
let r#type = id_parts[0].clone();
let id = id_parts[1]
.parse::<i64>()
.map_err(|_| Error::HttpBadRequest("Invalid ID".into()))?;
.map_err(|_| Error::InvalidInput("Invalid ID".into()))?;

pool.get()
.await?
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -136,7 +136,7 @@ impl From<base64::DecodeError> 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)]
Expand All @@ -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,
Expand Down

0 comments on commit 41d435c

Please sign in to comment.