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 8a23b39
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 28 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.

2 changes: 1 addition & 1 deletion crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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))
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),
}
9 changes: 6 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use swc_core::{
use swc_xml::parser::parse_file_as_document;

mod add_jsx_attribute;
mod error;
mod core;
mod hast_to_swc_ast;
mod remove_jsx_attribute;
Expand All @@ -28,6 +29,7 @@ mod transform_svg_component;

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

/// Transform SVG into React components.
///
Expand All @@ -50,18 +52,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
14 changes: 8 additions & 6 deletions crates/core/src/transform_svg_component/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use swc_core::{
ecma::{ast::*, parser},
};

use crate::SvgrError;

use super::core;

pub struct TemplateVariables {
Expand Down Expand Up @@ -58,7 +60,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 +315,7 @@ 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 +338,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 +356,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 @@ -397,8 +399,8 @@ fn get_jsx_runtime_import_specifiers(
}

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

Expand Down

0 comments on commit 8a23b39

Please sign in to comment.