-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor error with enum codes #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a0dd4f3
refactor error with enum codes
fnlctrl 9108b5c
fix lint
fnlctrl a4f14f1
rename error 'code' to 'type'
fnlctrl f6f7b26
refactor
fnlctrl ae0dc6a
Merge branch 'master' into refactor-error
fnlctrl f3644ae
remove INVALID_ROUTE_MATCH
fnlctrl 6717d26
fix: lint
fnlctrl ef17a9f
fix: remove ErrorTypeMessages in production build
fnlctrl 980d416
fix: types
fnlctrl 827b859
fix: add interface NavigationRedirectError
fnlctrl d823f1e
build: use similar config to vue
posva c34ddf6
refactor: merge
posva 156fcae
refactor: errors
posva 72667ca
build: add banner
posva 6301e72
refactor: delete old errors.ts
posva 7792cc3
refactor: rename errors-new
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Router Matcher resolve LocationAsName throws if the named route does not exists 1`] = ` | ||
[NoRouteMatchError: No match for | ||
{"name":"Home"}] | ||
[Error: No match for | ||
{"name":"Home"}] | ||
`; | ||
|
||
exports[`Router Matcher resolve LocationAsRelative throws if the current named route does not exists 1`] = ` | ||
[NoRouteMatchError: No match for | ||
{"params":{"a":"foo"}} | ||
[Error: No match for | ||
{"params":{"a":"foo"}} | ||
while being at | ||
{"name":"home","params":{},"path":"/","meta":{}}] | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
MatcherLocation, | ||
MatcherLocationNormalized, | ||
RouteLocation, | ||
RouteLocationNormalized, | ||
} from './types' | ||
|
||
// Using string enums because error codes are exposed to developers | ||
// and number enums could collide with other error codes in runtime | ||
export enum ErrorTypes { | ||
MATCHER_NOT_FOUND = 'MATCHER_NOT_FOUND', | ||
NAVIGATION_GUARD_REDIRECT = 'NAVIGATION_GUARD_REDIRECT', | ||
NAVIGATION_ABORTED = 'NAVIGATION_ABORTED', | ||
NAVIGATION_CANCELLED = 'NAVIGATION_CANCELLED', | ||
} | ||
|
||
interface RouterError extends Error { | ||
type: ErrorTypes | ||
} | ||
|
||
interface MatcherError extends RouterError { | ||
type: ErrorTypes.MATCHER_NOT_FOUND | ||
location: MatcherLocation | ||
currentLocation?: MatcherLocationNormalized | ||
} | ||
|
||
interface NavigationError extends RouterError { | ||
type: | ||
| ErrorTypes.NAVIGATION_GUARD_REDIRECT | ||
| ErrorTypes.NAVIGATION_ABORTED | ||
| ErrorTypes.NAVIGATION_CANCELLED | ||
from: RouteLocationNormalized | ||
to: RouteLocationNormalized | ||
} | ||
|
||
type InferErrorType<Type extends ErrorTypes> = Type extends MatcherError['type'] | ||
? MatcherError | ||
: Type extends NavigationError['type'] | ||
? NavigationError | ||
: never | ||
|
||
const ErrorTypeMessages = __DEV__ | ||
? { | ||
[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 }: NavigationError) { | ||
return `Redirected from "${from.fullPath}" to "${stringifyRoute( | ||
to | ||
)}" via a navigation guard` | ||
}, | ||
[ErrorTypes.NAVIGATION_ABORTED]({ from, to }: NavigationError) { | ||
return `Navigation aborted from "${from.fullPath}" to "${to.fullPath}" via a navigation guard` | ||
}, | ||
[ErrorTypes.NAVIGATION_CANCELLED]({ from, to }: NavigationError) { | ||
return `Navigation cancelled from "${from.fullPath}" to "${to.fullPath}" with a new \`push\` or \`replace\`` | ||
}, | ||
} | ||
: undefined | ||
|
||
export function createRouterError<Type extends ErrorTypes>( | ||
type: Type, | ||
params: Omit<InferErrorType<Type>, 'type' | keyof Error> | ||
): InferErrorType<Type> { | ||
const message = __DEV__ ? (ErrorTypeMessages as any)[type](params) : undefined | ||
const error = Object.assign(new Error(message), { type }, params) | ||
return (error as unknown) as InferErrorType<Type> | ||
} | ||
|
||
const propertiesToLog = ['params', 'query', 'hash'] | ||
|
||
function stringifyRoute(to: RouteLocation): string { | ||
if (typeof to === 'string') return to | ||
if ('path' in to) return to.path | ||
const location: Partial<RouteLocationNormalized> = {} | ||
for (const key of propertiesToLog) { | ||
// @ts-ignore | ||
if (key in to) location[key] = to[key] | ||
} | ||
return JSON.stringify(location, null, 2) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.