From c9778a314fab6a6b02da5b9ac8c21b8bb46450e7 Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Tue, 11 Feb 2025 21:56:15 +0900 Subject: [PATCH] feat: error handling. --- crates/schema-store/src/error.rs | 23 +++++++++----- crates/schema-store/src/json/catalog.rs | 10 ------- crates/schema-store/src/schema.rs | 18 ++++++++--- .../src/schema/referable_schema.rs | 30 +++++++++++-------- crates/schema-store/src/store.rs | 10 +++---- 5 files changed, 52 insertions(+), 39 deletions(-) diff --git a/crates/schema-store/src/error.rs b/crates/schema-store/src/error.rs index fac6d953..3afd058c 100644 --- a/crates/schema-store/src/error.rs +++ b/crates/schema-store/src/error.rs @@ -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}")] @@ -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 }, } diff --git a/crates/schema-store/src/json/catalog.rs b/crates/schema-store/src/json/catalog.rs index c06e9b9e..9dfda169 100644 --- a/crates/schema-store/src/json/catalog.rs +++ b/crates/schema-store/src/json/catalog.rs @@ -15,16 +15,6 @@ impl CatalogUrl { pub fn new(url: url::Url) -> Self { Self(url) } - - #[inline] - pub fn parse(s: &str) -> Result { - url::Url::parse(s).map(Self) - } - - #[inline] - pub fn from_file_path>(path: P) -> Result { - url::Url::from_file_path(&path).map(Self) - } } impl std::ops::Deref for CatalogUrl { diff --git a/crates/schema-store/src/schema.rs b/crates/schema-store/src/schema.rs index f34fb74a..e641a927 100644 --- a/crates/schema-store/src/schema.rs +++ b/crates/schema-store/src/schema.rs @@ -56,13 +56,23 @@ impl SchemaUrl { } #[inline] - pub fn parse(s: &str) -> Result { - url::Url::parse(s).map(Self) + pub fn parse(url: &str) -> Result { + 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>(path: P) -> Result { - url::Url::from_file_path(&path).map(Self) + pub fn from_file_path>(path: P) -> Result { + 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(), + }), + } } } diff --git a/crates/schema-store/src/schema/referable_schema.rs b/crates/schema-store/src/schema/referable_schema.rs index c3fdd8b6..6f7a55e1 100644 --- a/crates/schema-store/src/schema/referable_schema.rs +++ b/crates/schema-store/src/schema/referable_schema.rs @@ -80,21 +80,25 @@ impl Referable { 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(), + }) + } } } diff --git a/crates/schema-store/src/store.rs b/crates/schema-store/src/store.rs index c641016c..7fc69430 100644 --- a/crates/schema-store/src/store.rs +++ b/crates/schema-store/src/store.rs @@ -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}; @@ -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 { @@ -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 })?;