Skip to content

Commit 72eb556

Browse files
chore: use custom error class for navigation
1 parent db46f52 commit 72eb556

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
constructUrl,
@@ -119,7 +120,7 @@ export function createRouter(r) {
119120
function navigate(path, options = {}) {
120121
if (typeof path === 'number') {
121122
globalThis.history.go(path);
122-
return new Error(`Navigating to history entry: ${path}`);
123+
return new Navigation(`History entry: ${path}`);
123124
}
124125

125126
path = constructPath(path, options.params);
@@ -129,7 +130,7 @@ function navigate(path, options = {}) {
129130
options.hash = '#' + options.hash;
130131
}
131132
onNavigate(path, options);
132-
return new Error(`Navigating to: ${path}${options?.search ?? ''}${options?.hash ?? ''}`);
133+
return new Navigation(`${path}${options?.search ?? ''}${options?.hash ?? ''}`);
133134
}
134135

135136
/**

src/index.d.ts

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

109+
export class Navigation extends Error {
110+
constructor(target: string);
111+
}
112+
109113
export type RouterApi<T extends Routes> = {
110114
/**
111115
* 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,5 +1,6 @@
11
export { isActiveLink } from './actions.svelte.js';
22
export { createRouter } from './create-router.svelte.js';
33
export { serializeSearch } from './helpers/utils.js';
4+
export { Navigation } from './navigation.js';
45
export { default as Router } from './Router.svelte';
56
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)