forked from vuejs/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ts
193 lines (181 loc) · 5.41 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {
MatcherLocationRaw,
MatcherLocation,
RouteLocationRaw,
RouteLocationNormalized,
} from './types'
import { assign } from './utils'
import { PolySymbol } from './injectionSymbols'
/**
* Flags so we can combine them when checking for multiple errors
*/
export const enum ErrorTypes {
// they must be literals to be used as values so we can't write
// 1 << 2
MATCHER_NOT_FOUND = 1,
NAVIGATION_GUARD_REDIRECT = 2,
NAVIGATION_ABORTED = 4,
NAVIGATION_CANCELLED = 8,
NAVIGATION_DUPLICATED = 16,
}
const NavigationFailureSymbol = /*#__PURE__*/ PolySymbol(
__DEV__ ? 'navigation failure' : 'nf'
)
export interface MatcherError extends Error {
type: ErrorTypes.MATCHER_NOT_FOUND
location: MatcherLocationRaw
currentLocation?: MatcherLocation
}
/**
* Enumeration with all possible types for navigation failures. Can be passed to
* {@link isNavigationFailure} to check for specific failures.
*/
export enum NavigationFailureType {
/**
* An aborted navigation is a navigation that failed because a navigation
* guard returned `false` or called `next(false)`
*/
aborted = ErrorTypes.NAVIGATION_ABORTED,
/**
* A cancelled navigation is a navigation that failed because a more recent
* navigation finished started (not necessarily finished).
*/
cancelled = ErrorTypes.NAVIGATION_CANCELLED,
/**
* A duplicated navigation is a navigation that failed because it was
* initiated while already being at the exact same location.
*/
duplicated = ErrorTypes.NAVIGATION_DUPLICATED,
}
/**
* Extended Error that contains extra information regarding a failed navigation.
*/
export interface NavigationFailure extends Error {
/**
* Type of the navigation. One of {@link NavigationFailureType}
*/
type:
| ErrorTypes.NAVIGATION_CANCELLED
| ErrorTypes.NAVIGATION_ABORTED
| ErrorTypes.NAVIGATION_DUPLICATED
/**
* Route location we were navigating from
*/
from: RouteLocationNormalized
/**
* Route location we were navigating to
*/
to: RouteLocationNormalized
}
export interface NavigationRedirectError
extends Omit<NavigationFailure, 'to' | 'type'> {
type: ErrorTypes.NAVIGATION_GUARD_REDIRECT
to: RouteLocationRaw
}
// DEV only debug messages
const ErrorTypeMessages = {
[ErrorTypes.MATCHER_NOT_FOUND]({ location, currentLocation }: MatcherError) {
return `No match for\n ${JSON.stringify(location)}${
currentLocation
? '\nwhile being at\n' + JSON.stringify(currentLocation)
: ''
}`
},
[ErrorTypes.NAVIGATION_GUARD_REDIRECT]({
from,
to,
}: NavigationRedirectError) {
return `Redirected from "${from.fullPath}" to "${stringifyRoute(
to
)}" via a navigation guard.`
},
[ErrorTypes.NAVIGATION_ABORTED]({ from, to }: NavigationFailure) {
return `Navigation aborted from "${from.fullPath}" to "${to.fullPath}" via a navigation guard.`
},
[ErrorTypes.NAVIGATION_CANCELLED]({ from, to }: NavigationFailure) {
return `Navigation cancelled from "${from.fullPath}" to "${to.fullPath}" with a new navigation.`
},
[ErrorTypes.NAVIGATION_DUPLICATED]({ from, to }: NavigationFailure) {
return `Avoided redundant navigation to current location: "${from.fullPath}".`
},
}
// Possible internal errors
type RouterError = NavigationFailure | NavigationRedirectError | MatcherError
export function createRouterError<E extends RouterError>(
type: E['type'],
params: Omit<E, 'type' | keyof Error>
): E {
// keep full error messages in cjs versions
if (__DEV__ || !__BROWSER__) {
return assign(
new Error(ErrorTypeMessages[type](params as any)),
{
type,
[NavigationFailureSymbol]: true,
} as { type: typeof type },
params
) as E
} else {
return assign(
new Error(),
{
type,
[NavigationFailureSymbol]: true,
} as { type: typeof type },
params
) as E
}
}
/**
* Check if an object is a {@link NavigationFailure}.
*
* @example
* ```js
* import { isNavigationFailure, NavigationFailureType } from 'vue-router'
*
* router.afterEach((to, from, failure) => {
* // Any kind of navigation failure
* if (isNavigationFailure(failure)) {
* // ...
* }
* // Only duplicated navigations
* if (isNavigationFailure(failure, NavigationFailureType.duplicated)) {
* // ...
* }
* // Aborted or canceled navigations
* if (isNavigationFailure(failure, NavigationFailureType.aborted | NavigationFailureType.canceled)) {
* // ...
* }
* })
* ```
* @param error - possible {@link NavigationFailure}
* @param type - optional types to check for
*/
export function isNavigationFailure(
error: any,
type?: ErrorTypes.NAVIGATION_GUARD_REDIRECT
): error is NavigationRedirectError
export function isNavigationFailure(
error: any,
type?: ErrorTypes | NavigationFailureType
): error is NavigationFailure
export function isNavigationFailure(
error: any,
type?: number
): error is NavigationFailure {
return (
error instanceof Error &&
NavigationFailureSymbol in error &&
(type == null || !!((error as NavigationFailure).type & type))
)
}
const propertiesToLog = ['params', 'query', 'hash'] as const
function stringifyRoute(to: RouteLocationRaw): string {
if (typeof to === 'string') return to
if ('path' in to) return to.path
const location = {} as Record<string, unknown>
for (const key of propertiesToLog) {
if (key in to) location[key] = to[key]
}
return JSON.stringify(location, null, 2)
}