Skip to content

Commit

Permalink
feat: don't panic for invalid route path
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlin1228 committed Oct 9, 2024
1 parent a7f3dfa commit 739d48e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions crates/dt_route/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::bail;
use dt_parser::{
anonymous_default_export::SYMBOL_NAME_FOR_ANONYMOUS_DEFAULT_EXPORT, types::SymbolDependency,
};
Expand Down Expand Up @@ -118,7 +119,7 @@ impl RouteVisitor {
}
}

fn get_route_path(&self, route_object: &Expr) -> String {
fn get_route_path(&self, route_object: &Expr) -> anyhow::Result<String> {
match route_object {
Expr::Object(object_lit) => {
for prop in object_lit.props.iter() {
Expand All @@ -129,10 +130,10 @@ impl RouteVisitor {
if ident_name.sym == "path" {
match &**value {
Expr::Lit(lit) => match lit {
Lit::Str(s) => return s.value.to_string(),
_ => panic!("path value is literal but not string literal for {}", self.module_path)
Lit::Str(s) => return Ok(s.value.to_string()),
_ => bail!("path value is literal but not string literal for {}", self.module_path)
},
_ => panic!(
_ => bail!(
"failed to transform value to string for {}",
self.module_path
),
Expand All @@ -147,9 +148,9 @@ impl RouteVisitor {
}
}
}
_ => panic!("invalid route object for {}", self.module_path),
_ => bail!("invalid route object for {}", self.module_path),
}
panic!(
bail!(
"failed to find the path in the route object for {}",
self.module_path
);
Expand All @@ -176,13 +177,17 @@ impl Visit for RouteVisitor {
match prop {
PropOrSpread::Prop(prop) => match &**prop {
Prop::KeyValue(KeyValueProp { value, .. }) => {
let route_path = self.get_route_path(&**value);
self.current_route_path = Some(Route {
path: route_path,
depend_on: HashSet::new(),
});
value.visit_children_with(self);
self.routes.push(self.current_route_path.take().unwrap());
match self.get_route_path(&**value) {
Ok(route_path) => {
self.current_route_path = Some(Route {
path: route_path,
depend_on: HashSet::new(),
});
value.visit_children_with(self);
self.routes.push(self.current_route_path.take().unwrap());
}
Err(_) => (),
}
}
_ => (),
},
Expand Down

0 comments on commit 739d48e

Please sign in to comment.