Skip to content

Commit

Permalink
refactor: isRouterError -> isNavigationFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 31, 2020
1 parent b651040 commit 9f5989e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/history/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type Router from '../index'
import { History } from './base'
import { NavigationFailureType, isRouterError } from '../util/errors'
import { NavigationFailureType, isNavigationFailure } from '../util/errors'

export class AbstractHistory extends History {
index: number
Expand Down Expand Up @@ -50,7 +50,7 @@ export class AbstractHistory extends History {
this.updateRoute(route)
},
err => {
if (isRouterError(err, NavigationFailureType.duplicated)) {
if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
this.index = targetIndex
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
createNavigationRedirectedError,
createNavigationAbortedError,
isError,
isRouterError,
isNavigationFailure,
NavigationFailureType
} from '../util/errors'

Expand All @@ -37,7 +37,11 @@ export class History {
// implemented by sub-classes
+go: (n: number) => void
+push: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void
+replace: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void
+replace: (
loc: RawLocation,
onComplete?: Function,
onAbort?: Function
) => void
+ensureURL: (push?: boolean) => void
+getCurrentLocation: () => string
+setupListeners: Function
Expand Down Expand Up @@ -117,7 +121,7 @@ export class History {
this.ready = true
// Initial redirection should still trigger the onReady onSuccess
// https://github.com/vuejs/vue-router/issues/3225
if (!isRouterError(err, NavigationFailureType.redirected)) {
if (!isNavigationFailure(err, NavigationFailureType.redirected)) {
this.readyErrorCbs.forEach(cb => {
cb(err)
})
Expand All @@ -137,7 +141,7 @@ export class History {
// changed after adding errors with
// https://github.com/vuejs/vue-router/pull/3047 before that change,
// redirect and aborted navigation would produce an err == null
if (!isRouterError(err) && isError(err)) {
if (!isNavigationFailure(err) && isError(err)) {
if (this.errorCbs.length) {
this.errorCbs.forEach(cb => {
cb(err)
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { AbstractHistory } from './history/abstract'

import type { Matcher } from './create-matcher'

import { isRouterError, NavigationFailureType } from './util/errors'
import { isNavigationFailure, NavigationFailureType } from './util/errors'

export default class VueRouter {
static install: () => void
static version: string
static isRouterError: Function
static isNavigationFailure: Function
static NavigationFailureType: any

app: any
Expand Down Expand Up @@ -120,7 +120,7 @@ export default class VueRouter {
const history = this.history

if (history instanceof HTML5History || history instanceof HashHistory) {
const handleInitialScroll = (routeOrError) => {
const handleInitialScroll = routeOrError => {
const from = history.current
const expectScroll = this.options.scrollBehavior
const supportsScroll = supportsPushState && expectScroll
Expand All @@ -129,7 +129,7 @@ export default class VueRouter {
handleScroll(this, routeOrError, from, false)
}
}
const setupListeners = (routeOrError) => {
const setupListeners = routeOrError => {
history.setupListeners()
handleInitialScroll(routeOrError)
}
Expand Down Expand Up @@ -271,7 +271,7 @@ function createHref (base: string, fullPath: string, mode) {

VueRouter.install = install
VueRouter.version = '__VERSION__'
VueRouter.isRouterError = isRouterError
VueRouter.isNavigationFailure = isNavigationFailure
VueRouter.NavigationFailureType = NavigationFailureType

if (inBrowser && window.Vue) {
Expand Down
8 changes: 6 additions & 2 deletions src/util/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export function isError (err) {
return Object.prototype.toString.call(err).indexOf('Error') > -1
}

export function isRouterError (err, errorType) {
return isError(err) && err._isRouter && (errorType == null || err.type === errorType)
export function isNavigationFailure (err, errorType) {
return (
isError(err) &&
err._isRouter &&
(errorType == null || err.type === errorType)
)
}

0 comments on commit 9f5989e

Please sign in to comment.