Skip to content

Commit

Permalink
feat(projects): add error handle when route resolve failed
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed May 19, 2024
1 parent 37be7d2 commit 356127e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 66 deletions.
84 changes: 51 additions & 33 deletions examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,27 @@ function transformElegantRouteToVueRoute(
}

function getLayoutName(component: string) {
return component.replace(LAYOUT_PREFIX, '');
const layout = component.replace(LAYOUT_PREFIX, '');

if(!layouts[layout]) {
throw new Error(`Layout component "${layout}" not found`);
}

return layout;
}

function isView(component: string) {
return component.startsWith(VIEW_PREFIX);
}

function getViewName(component: string) {
return component.replace(VIEW_PREFIX, '');
const view = component.replace(VIEW_PREFIX, '');

if(!views[view]) {
throw new Error(`View component "${view}" not found`);
}

return view;
}

function isFirstLevelRoute(item: ElegantConstRoute) {
Expand Down Expand Up @@ -81,39 +93,45 @@ function transformElegantRouteToVueRoute(

const vueRoute = { name, path, ...rest } as RouteRecordRaw;

if (component) {
if (isSingleLevelRoute(route)) {
const { layout, view } = getSingleLevelRouteComponent(component);

const singleLevelRoute: RouteRecordRaw = {
path,
component: layouts[layout],
children: [
{
name,
path: '',
component: views[view],
...rest
} as RouteRecordRaw
]
};

return [singleLevelRoute];
}

if (isLayout(component)) {
const layoutName = getLayoutName(component);

vueRoute.component = layouts[layoutName];
}

if (isView(component)) {
const viewName = getViewName(component);

vueRoute.component = views[viewName];
try {
if (component) {
if (isSingleLevelRoute(route)) {
const { layout, view } = getSingleLevelRouteComponent(component);

const singleLevelRoute: RouteRecordRaw = {
path,
component: layouts[layout],
children: [
{
name,
path: '',
component: views[view],
...rest
} as RouteRecordRaw
]
};

return [singleLevelRoute];
}

if (isLayout(component)) {
const layoutName = getLayoutName(component);

vueRoute.component = layouts[layoutName];
}

if (isView(component)) {
const viewName = getViewName(component);

vueRoute.component = views[viewName];
}

}

} catch (error: any) {
console.error(`Error transforming route "${route.name}": ${error.toString()}`);
return [];
}


// add redirect to child
if (children?.length && !vueRoute.redirect) {
Expand Down
84 changes: 51 additions & 33 deletions packages/vue/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,27 @@ function transformElegantRouteToVueRoute(
}
function getLayoutName(component: string) {
return component.replace(LAYOUT_PREFIX, '');
const layout = component.replace(LAYOUT_PREFIX, '');
if(!layouts[layout]) {
throw new Error(\`Layout component "\${layout}" not found\`);
}
return layout;
}
function isView(component: string) {
return component.startsWith(VIEW_PREFIX);
}
function getViewName(component: string) {
return component.replace(VIEW_PREFIX, '');
const view = component.replace(VIEW_PREFIX, '');
if(!views[view]) {
throw new Error(\`View component "\${view}" not found\`);
}
return view;
}
function isFirstLevelRoute(item: ElegantConstRoute) {
Expand Down Expand Up @@ -108,39 +120,45 @@ function transformElegantRouteToVueRoute(
const vueRoute = { name, path, ...rest } as RouteRecordRaw;
if (component) {
if (isSingleLevelRoute(route)) {
const { layout, view } = getSingleLevelRouteComponent(component);
const singleLevelRoute: RouteRecordRaw = {
path,
component: layouts[layout],
children: [
{
name,
path: '',
component: views[view],
...rest
} as RouteRecordRaw
]
};
return [singleLevelRoute];
}
if (isLayout(component)) {
const layoutName = getLayoutName(component);
vueRoute.component = layouts[layoutName];
}
if (isView(component)) {
const viewName = getViewName(component);
vueRoute.component = views[viewName];
try {
if (component) {
if (isSingleLevelRoute(route)) {
const { layout, view } = getSingleLevelRouteComponent(component);
const singleLevelRoute: RouteRecordRaw = {
path,
component: layouts[layout],
children: [
{
name,
path: '',
component: views[view],
...rest
} as RouteRecordRaw
]
};
return [singleLevelRoute];
}
if (isLayout(component)) {
const layoutName = getLayoutName(component);
vueRoute.component = layouts[layoutName];
}
if (isView(component)) {
const viewName = getViewName(component);
vueRoute.component = views[viewName];
}
}
} catch (error: any) {
console.error(\`Error transforming route "\${route.name}": \${error.toString()}\`);
return [];
}
// add redirect to child
if (children?.length && !vueRoute.redirect) {
Expand Down

0 comments on commit 356127e

Please sign in to comment.