Skip to content

Commit 8e241af

Browse files
chore: use custom error class for navigation
1 parent ace70ee commit 8e241af

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

src/create-router.svelte.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { matchRoute } from './helpers/match-route.js';
44
import { preload, preloadOnHover } from './helpers/preload.js';
55
import { constructPath, join, resolveRouteComponents, updatedLocation } from './helpers/utils.js';
66
import { syncSearchParams } from './search-params.svelte.js';
7+
import { Navigation } from './navigation.js';
78

89
/** @type {import('./index.d.ts').Routes} */
910
let routes;
@@ -86,7 +87,7 @@ export function createRouter(r) {
8687
function navigate(path, options = {}) {
8788
if (typeof path === 'number') {
8889
globalThis.history.go(path);
89-
return new Error(`Navigating to history entry: ${path}`);
90+
return new Navigation(`History entry: ${path}`);
9091
}
9192
if (options.params) {
9293
path = constructPath(path, options.params);
@@ -98,7 +99,7 @@ function navigate(path, options = {}) {
9899
options.hash = '#' + options.hash;
99100
}
100101
onNavigate(path, options);
101-
return new Error(`Navigating to: ${path}${options?.search ?? ''}${options?.hash ?? ''}`);
102+
return new Navigation(`${path}${options?.search ?? ''}${options?.hash ?? ''}`);
102103
}
103104

104105
/**

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export type IsActiveLink = Action<
9696
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
9797
export interface RouteMeta {}
9898

99+
export class Navigation extends Error {
100+
constructor(target: string);
101+
}
102+
99103
export type RouterApi<T extends Routes> = {
100104
/**
101105
* Construct a path while ensuring type safety.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { isActiveLink } from './actions.svelte.js';
22
export { createRouter } from './create-router.svelte.js';
33
export { default as Router } from './Router.svelte';
44
export { searchParams } from './search-params.svelte.js';
5+
export { Navigation } from 'navigation.js';

src/navigation.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class Navigation extends Error {
2+
constructor(
3+
/** @type {string} target */
4+
target,
5+
) {
6+
super(`Navigating to: ${target}`);
7+
this.name = 'Redirect';
8+
}
9+
}

0 commit comments

Comments
 (0)