Skip to content

Commit

Permalink
feat: error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Feb 11, 2025
1 parent c9778a3 commit 748e697
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
20 changes: 13 additions & 7 deletions crates/schema-store/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ pub enum Error {
#[error("failed to read schema: \"{schema_path}\"")]
SchemaFileReadFailed { schema_path: PathBuf },

#[error("invalid schema file: \"{schema_url}\"")]
SchemaFileParseFailed { schema_url: SchemaUrl },

#[error("failed to fetch schema: {schema_url}")]
SchemaFetchFailed { schema_url: SchemaUrl },
#[error("failed to parse schema: {schema_url}, reason: {reason}")]
SchemaFileParseFailed {
schema_url: SchemaUrl,
reason: String,
},

#[error("failed to fetch schema: {schema_url}, reason: {reason}")]
SchemaFetchFailed {
schema_url: SchemaUrl,
reason: String,
},

#[error("unsupported source url: {source_url}")]
SourceUrlUnsupported { source_url: url::Url },
Expand All @@ -43,8 +49,8 @@ pub enum Error {
#[error("invalid file path: {url}")]
InvalidFilePath { url: url::Url },

#[error("invalid json format: {url}")]
InvalidJsonFormat { url: url::Url },
#[error("invalid json format: {url}, reason: {reason}")]
InvalidJsonFormat { url: url::Url, reason: String },

#[error("invalid json schema reference: {reference}")]
InvalidJsonSchemaReference { reference: String },
Expand Down
43 changes: 24 additions & 19 deletions crates/schema-store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,27 @@ impl SchemaStore {
tracing::debug!("loading schema catalog: {}", catalog_url);

if let Ok(response) = self.http_client.get(catalog_url.as_str()).send().await {
if let Ok(catalog) = response.json::<crate::json::Catalog>().await {
let mut catalogs = self.catalogs.write().await;
for catalog_schema in catalog.schemas {
if catalog_schema
.file_match
.iter()
.any(|pattern| pattern.ends_with(".toml"))
{
catalogs.push(crate::CatalogSchema {
url: catalog_schema.url,
include: catalog_schema.file_match,
});
match response.json::<crate::json::Catalog>().await {
Ok(catalog) => {
let mut catalogs = self.catalogs.write().await;
for catalog_schema in catalog.schemas {
if catalog_schema
.file_match
.iter()
.any(|pattern| pattern.ends_with(".toml"))
{
catalogs.push(crate::CatalogSchema {
url: catalog_schema.url,
include: catalog_schema.file_match,
});
}
}
Ok(())
}
Ok(())
} else {
Err(crate::Error::InvalidJsonFormat {
Err(err) => Err(crate::Error::InvalidJsonFormat {
url: catalog_url.deref().clone(),
})
reason: err.to_string(),
}),
}
} else {
Err(crate::Error::CatalogUrlFetchFailed {
Expand Down Expand Up @@ -134,16 +136,18 @@ impl SchemaStore {
.get(schema_url.as_str())
.send()
.await
.map_err(|_| crate::Error::SchemaFetchFailed {
.map_err(|err| crate::Error::SchemaFetchFailed {
schema_url: schema_url.clone(),
reason: err.to_string(),
})?;

let bytes =
response
.bytes()
.await
.map_err(|_| crate::Error::SchemaFetchFailed {
.map_err(|err| crate::Error::SchemaFetchFailed {
schema_url: schema_url.clone(),
reason: err.to_string(),
})?;

serde_json::from_reader(std::io::Cursor::new(bytes))
Expand All @@ -154,8 +158,9 @@ impl SchemaStore {
})
}
}
.map_err(|_| crate::Error::SchemaFileParseFailed {
.map_err(|err| crate::Error::SchemaFileParseFailed {
schema_url: schema_url.to_owned(),
reason: err.to_string(),
})?;

Ok(DocumentSchema::new(schema, schema_url.clone()))
Expand Down

0 comments on commit 748e697

Please sign in to comment.