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 7c4b10f commit c9778a3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
23 changes: 16 additions & 7 deletions crates/schema-store/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ pub enum Error {
#[error("definition ref not found: {definition_ref}")]
DefinitionNotFound { definition_ref: String },

#[error("failed to parse catalog: {catalog_url}")]
CatalogUrlParseFailed { catalog_url: CatalogUrl },

#[error("failed to fetch catalog: {catalog_url}")]
CatalogUrlFetchFailed { catalog_url: CatalogUrl },

#[error("unsupported schema url: {schema_url}")]
SchemaUrlUnsupported { schema_url: SchemaUrl },

#[error("failed to parse schema url: {schema_url}")]
SchemaUrlParseFailed { schema_url: SchemaUrl },
#[error("invalid schema url: {schema_url}")]
InvalidSchemaUrl { schema_url: String },

#[error("failed to read schema: \"{schema_path}\"")]
SchemaFileReadFailed { schema_path: PathBuf },

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

#[error("failed to fetch schema: {schema_url}")]
Expand All @@ -40,6 +37,18 @@ pub enum Error {
#[error("unsupported source url: {source_url}")]
SourceUrlUnsupported { source_url: url::Url },

#[error("failed to parse source url: {source_url}")]
#[error("invalid source url: {source_url}")]
SourceUrlParseFailed { source_url: url::Url },

#[error("invalid file path: {url}")]
InvalidFilePath { url: url::Url },

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

#[error("invalid json schema reference: {reference}")]
InvalidJsonSchemaReference { reference: String },

#[error("unsupported reference: {reference}")]
UnsupportedReference { reference: String },
}
10 changes: 0 additions & 10 deletions crates/schema-store/src/json/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ impl CatalogUrl {
pub fn new(url: url::Url) -> Self {
Self(url)
}

#[inline]
pub fn parse(s: &str) -> Result<Self, url::ParseError> {
url::Url::parse(s).map(Self)
}

#[inline]
pub fn from_file_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, ()> {
url::Url::from_file_path(&path).map(Self)
}
}

impl std::ops::Deref for CatalogUrl {
Expand Down
18 changes: 14 additions & 4 deletions crates/schema-store/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ impl SchemaUrl {
}

#[inline]
pub fn parse(s: &str) -> Result<Self, url::ParseError> {
url::Url::parse(s).map(Self)
pub fn parse(url: &str) -> Result<Self, crate::Error> {
match url::Url::parse(url) {
Ok(url) => Ok(Self(url)),
Err(_) => Err(crate::Error::InvalidSchemaUrl {
schema_url: url.to_string(),
}),
}
}

#[inline]
pub fn from_file_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, ()> {
url::Url::from_file_path(&path).map(Self)
pub fn from_file_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self, crate::Error> {
match url::Url::from_file_path(&path) {
Ok(url) => Ok(Self(url)),
Err(_) => Err(crate::Error::InvalidSchemaUrl {
schema_url: path.as_ref().to_string_lossy().to_string(),
}),
}
}
}

Expand Down
30 changes: 17 additions & 13 deletions crates/schema-store/src/schema/referable_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,25 @@ impl Referable<ValueSchema> {
if http_url.starts_with("https://")
|| http_url.starts_with("http://") =>
{
if let Ok(schema_url) = SchemaUrl::parse(http_url) {
if let Ok(document_schema) =
schema_store.try_load_document_schema(&schema_url).await
{
if let Some(value_schema) = document_schema.value_schema {
*self = Referable::Resolved(value_schema);
new_schema = Some((
schema_url,
document_schema.definitions.clone(),
));
}
}
let schema_url = SchemaUrl::parse(http_url)?;
let document_schema =
schema_store.try_load_document_schema(&schema_url).await?;

if let Some(value_schema) = document_schema.value_schema {
*self = Referable::Resolved(value_schema);
new_schema =
Some((schema_url, document_schema.definitions.clone()));
} else {
return Err(crate::Error::InvalidJsonSchemaReference {
reference: reference.to_owned(),
});
}
}
_ => {}
_ => {
return Err(crate::Error::UnsupportedReference {
reference: reference.to_owned(),
})
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/schema-store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ahash::AHashMap;
use config::SchemaCatalogItem;
use itertools::Either;
use std::sync::Arc;
use std::{ops::Deref, sync::Arc};
use tokio::sync::RwLock;

use crate::{json::CatalogUrl, DocumentSchema, SchemaUrl};
Expand Down Expand Up @@ -84,8 +84,8 @@ impl SchemaStore {
}
Ok(())
} else {
Err(crate::Error::CatalogUrlParseFailed {
catalog_url: catalog_url.clone(),
Err(crate::Error::InvalidJsonFormat {
url: catalog_url.deref().clone(),
})
}
} else {
Expand Down Expand Up @@ -118,8 +118,8 @@ impl SchemaStore {
let schema_path =
schema_url
.to_file_path()
.map_err(|_| crate::Error::SchemaUrlParseFailed {
schema_url: schema_url.to_owned(),
.map_err(|_| crate::Error::InvalidFilePath {
url: schema_url.deref().to_owned(),
})?;
let file = std::fs::File::open(&schema_path)
.map_err(|_| crate::Error::SchemaFileReadFailed { schema_path })?;
Expand Down

0 comments on commit c9778a3

Please sign in to comment.