Skip to content

Commit 8718389

Browse files
chore: use custom error class for navigation
1 parent 69147f6 commit 8718389

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
@@ -2,6 +2,7 @@ import { BROWSER, DEV } from 'esm-env';
22
import { isActive } from './helpers/is-active.js';
33
import { matchRoute } from './helpers/match-route.js';
44
import { preload, preloadOnHover } from './helpers/preload.js';
5+
import { Navigation } from './navigation.js';
56
import {
67
constructPath,
78
join,
@@ -113,7 +114,7 @@ export function createRouter(r) {
113114
function navigate(path, options = {}) {
114115
if (typeof path === 'number') {
115116
globalThis.history.go(path);
116-
return new Error(`Navigating to history entry: ${path}`);
117+
return new Navigation(`History entry: ${path}`);
117118
}
118119

119120
path = constructPath(path, options.params);
@@ -127,7 +128,7 @@ function navigate(path, options = {}) {
127128
path = new URL(path).hash;
128129
}
129130
onNavigate(path, options);
130-
return new Error(`Navigating to: ${path}${options?.search ?? ''}${options?.hash ?? ''}`);
131+
return new Navigation(`${path}${options?.search ?? ''}${options?.hash ?? ''}`);
131132
}
132133

133134
/** @param {string} [path] */

src/index.d.ts

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

106+
export class Navigation extends Error {
107+
constructor(target: string);
108+
}
109+
106110
export type RouterApi<T extends Routes> = {
107111
/**
108112
* Construct a path while ensuring type safety.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { isActiveLink } from './actions.svelte.js';
22
export { createRouter } from './create-router.svelte.js';
3+
export { Navigation } from './navigation.js';
34
export { default as Router } from './Router.svelte';
45
export { searchParams } from './search-params.svelte.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)