Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
Signed-off-by: bkioshn <[email protected]>
  • Loading branch information
bkioshn committed Oct 17, 2024
1 parent ffa2dec commit 1d6fa85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions catalyst-gateway/bin/src/db/event/config/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ static FRONTEND_IP_DEFAULT: LazyLock<Value> =
/// Helper function to create a JSON validator from a JSON schema.
/// If the schema is invalid, a default JSON validator is created.
fn schema_validator(schema: &Value) -> Validator {
jsonschema::validator_for(schema).unwrap_or_else(|e| {
error!("Error creating JSON validator: {}", e);
jsonschema::validator_for(schema).unwrap_or_else(|err| {
error!(
id = "schema_validator",
errors=?err,
"Error creating JSON validator"
);

// Create a default JSON validator as a fallback
// This should not fail since it is hard coded
Expand All @@ -59,8 +63,8 @@ fn schema_validator(schema: &Value) -> Validator {

/// Helper function to convert a JSON string to a JSON value.
fn load_json_lazy(data: &str) -> Value {
serde_json::from_str(data).unwrap_or_else(|e| {
error!("Error parsing JSON: {}", e);
serde_json::from_str(data).unwrap_or_else(|err| {
error!(id = "load_json_lazy", errors=?err, "Error parsing JSON");
json!({})
})
}
Expand Down
15 changes: 10 additions & 5 deletions catalyst-gateway/bin/src/service/api/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ enum Responses {
/// Bad request errors
#[derive(Object, Default)]
struct BadRequestError {
/// List of errors
/// Error messages.
#[oai(validator(max_length = "100", pattern = "^[0-9a-zA-Z].*$"))]
error: String,
/// Optional schema validation errors.
#[oai(validator(max_items = "1000", max_length = "9999", pattern = "^[0-9a-zA-Z].*$"))]
errors: Vec<String>,
schema_validation_errors: Option<Vec<String>>,
}

/// All responses.
Expand Down Expand Up @@ -116,7 +119,8 @@ impl ConfigApi {
Ok(parsed_ip) => set(ConfigKey::FrontendForIp(parsed_ip), body_value).await,
Err(err) => {
Responses::BadRequest(Json(BadRequestError {
errors: vec![format!("Invalid IP address: {err}")],
error: format!("Invalid IP address: {err}"),
schema_validation_errors: None,
}))
.into()
},
Expand Down Expand Up @@ -153,12 +157,13 @@ async fn set(key: ConfigKey, value: Value) -> AllResponses {
match validate {
BasicOutput::Valid(_) => Responses::Ok(Json(json!(null))).into(),
BasicOutput::Invalid(errors) => {
let error_descriptions: Vec<String> = errors
let schema_errors: Vec<String> = errors
.iter()
.map(|error| error.error_description().clone().into_inner())
.collect();
Responses::BadRequest(Json(BadRequestError {
errors: error_descriptions,
error: "Invalid JSON data validating against JSON schema".to_string(),
schema_validation_errors: Some(schema_errors),
}))
.into()
},
Expand Down

0 comments on commit 1d6fa85

Please sign in to comment.