Skip to content

Error Handling

Uni Router provides a unified error handling mechanism. All navigation errors are wrapped in the NavigationFailure class with structured error codes.

Error Types

RouterError

Base route error class, containing error code and message:

ts
class RouterError extends Error {
	readonly code: RouterErrorCode
	readonly message: string
}

Navigation failure class, extending RouterError, with additional source and target route information:

ts
class NavigationFailure extends RouterError {
	readonly to: RouteLocation
	readonly from: RouteLocation
	readonly cause?: unknown
}

Error Codes

Error CodeDescriptionTrigger
NAVIGATION_ABORTEDNavigation aborted by guardGuard calls next(false)
NAVIGATION_CANCELLEDNavigation cancelledGuard throws exception or redirect limit exceeded
NAVIGATION_DUPLICATEDDuplicate navigationpush() to a page already at
ROUTE_NOT_FOUNDRoute not foundUsing undefined named route in strict mode
NAVIGATION_API_ERRORuni API call faileduni.navigateTo etc. call failed
SETUP_ERRORSetup erroruseRouter() called outside setup

router.onError()

Register a global error handler callback. All navigation errors trigger it:

ts
const removeHandler = router.onError((error, to, from) => {
	switch (error.code) {
		case 'NAVIGATION_ABORTED':
			console.log('Navigation aborted')
			break
		case 'NAVIGATION_DUPLICATED':
			console.log('Duplicate navigation, ignoring')
			break
		case 'NAVIGATION_API_ERROR':
			console.error('uni API call failed', error.cause)
			break
	}
})

// Remove the handler
removeHandler()

Exceptions in onError do not affect the execution of other error handlers. :::

try-catch Handling

You can also use try-catch when calling navigation methods:

ts
try {
	await router.push({ name: 'about' })
} catch (error) {
	if (error.code === 'NAVIGATION_DUPLICATED') {
		// Ignore duplicate navigation
		return
	}
	if (error.code === 'NAVIGATION_ABORTED') {
		console.log('Navigation aborted by guard')
		return
	}
	throw error
}

Common Error Handling Scenarios

Ignore Duplicate Navigation

ts
router.onError(error => {
	if (error.code === 'NAVIGATION_DUPLICATED') return
	console.error(error)
})

Login Redirect

ts
router.onError((error, to) => {
	if (error.code === 'NAVIGATION_ABORTED' && to.meta.requireAuth) {
		router.push({ name: 'login' })
	}
})

uni API Failure Retry

ts
router.onError(async (error, to) => {
	if (error.code === 'NAVIGATION_API_ERROR') {
		console.error('Navigation failed, target:', to.fullPath)
	}
})

Released under the MIT License.