Skip to content

Commit

Permalink
feat: more elegant error
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jan 14, 2024
1 parent 3809bf1 commit 60d4722
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub async fn transform_node(
};
match transform(code, config, state) {
Ok(result) => napi::bindgen_prelude::Result::Ok(result),
Err(reason) => {
napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(reason))
}
Err(reason) => napi::bindgen_prelude::Result::Err(napi::bindgen_prelude::Error::from_reason(
reason.to_string(),
)),
}
}
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ swc_core = { version = "0.74.2", features = [
"ecma_codegen",
"__parser",
] }
thiserror = "1.0.56"
linked-hash-map = { version = "0.5.6", features = ["serde_impl"] }
linked_hash_set = "0.1.4"

Expand Down
11 changes: 11 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum SvgrError {
#[error("failed to parse SVG: {0}")]
Parse(String),
#[error("this is invalid SVG")]
InvalidSvg,
#[error("invalid configuration option: {0}")]
Configuration(String),
}
10 changes: 7 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use swc_xml::parser::parse_file_as_document;

mod add_jsx_attribute;
mod core;
mod error;
mod hast_to_swc_ast;
mod remove_jsx_attribute;
mod replace_jsx_attribute;
Expand All @@ -26,6 +27,8 @@ mod svg_em_dimensions;
mod transform_react_native_svg;
mod transform_svg_component;

pub use error::SvgrError;

pub use self::core::config::{Config, ExpandProps, ExportType, Icon, JSXRuntime, JSXRuntimeImport};
pub use self::core::state::{Caller, Config as State};

Expand All @@ -50,18 +53,19 @@ pub use self::core::state::{Caller, Config as State};
/// Default::default(),
/// );
/// ```
pub fn transform(code: String, config: Config, state: State) -> Result<String, String> {
pub fn transform(code: String, config: Config, state: State) -> Result<String, SvgrError> {
let state = core::state::expand_state(&state);

let cm = Arc::<SourceMap>::default();
let fm = cm.new_source_file(FileName::Anon, code);

let mut errors = vec![];
let document = parse_file_as_document(fm.borrow(), Default::default(), &mut errors).unwrap();
let document = parse_file_as_document(fm.borrow(), Default::default(), &mut errors)
.map_err(|e| SvgrError::Parse(e.message().to_string()))?;

let jsx_element = hast_to_swc_ast::to_swc_ast(document);
if jsx_element.is_none() {
return Err("This is invalid SVG".to_string());
return Err(SvgrError::InvalidSvg);
}
let jsx_element = jsx_element.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/transform_svg_component/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use swc_core::{common::DUMMY_SP, ecma::ast::*};

use crate::core;
use crate::{core, SvgrError};

mod variables;

Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn transform(
jsx_element: JSXElement,
config: &core::config::Config,
state: &core::state::InternalConfig,
) -> Result<Module, String> {
) -> Result<Module, SvgrError> {
let variables_options = get_variables_options(config);

let variables = variables::get_variables(variables_options, state, jsx_element)?;
Expand Down
15 changes: 9 additions & 6 deletions crates/core/src/transform_svg_component/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use swc_core::{
};

use super::core;
use crate::SvgrError;

pub struct TemplateVariables {
pub component_name: String,
Expand Down Expand Up @@ -58,7 +59,7 @@ pub fn get_variables(
opts: Options,
state: &core::state::InternalConfig,
jsx: JSXElement,
) -> Result<TemplateVariables, String> {
) -> Result<TemplateVariables, SvgrError> {
let mut interfaces = vec![];
let mut props = vec![];
let mut imports = vec![];
Expand Down Expand Up @@ -313,7 +314,9 @@ pub fn get_variables(
}
}
} else {
return Err(r#""namedExport" not specified"#.to_string());
return Err(SvgrError::Configuration(
r#""namedExport" not specified"#.to_string(),
));
}
}

Expand All @@ -336,7 +339,7 @@ pub fn get_variables(
})
}

fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result<ModuleItem, String> {
fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result<ModuleItem, SvgrError> {
let specifiers = get_jsx_runtime_import_specifiers(cfg)?;

Ok(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
Expand All @@ -354,7 +357,7 @@ fn get_jsx_runtime_import(cfg: &core::config::JSXRuntimeImport) -> Result<Module

fn get_jsx_runtime_import_specifiers(
cfg: &core::config::JSXRuntimeImport,
) -> Result<Vec<ImportSpecifier>, String> {
) -> Result<Vec<ImportSpecifier>, SvgrError> {
if let Some(namespace) = cfg.namespace.clone() {
let specifier = ImportSpecifier::Namespace(ImportStarAsSpecifier {
span: DUMMY_SP,
Expand Down Expand Up @@ -396,10 +399,10 @@ fn get_jsx_runtime_import_specifiers(
return Ok(import_specifiers);
}

Err(
Err(SvgrError::Configuration(
r#"Specify "namespace", "defaultSpecifier", or "specifiers" in "jsxRuntimeImport" option"#
.to_string(),
)
))
}

fn get_or_create_import(
Expand Down

0 comments on commit 60d4722

Please sign in to comment.