242a25bfc4380fed7dcc2fad124a2b06473d1333f6b0f793529e3dbf1b6a9e307fd9cfef11adee72d3e1008994b08e4eed1d1028ed907b3154a5ad3a1424da 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // When changing thing, also edit router.d.ts
  2. export const NavigationFailureType = {
  3. redirected: 2,
  4. aborted: 4,
  5. cancelled: 8,
  6. duplicated: 16
  7. }
  8. export function createNavigationRedirectedError (from, to) {
  9. return createRouterError(
  10. from,
  11. to,
  12. NavigationFailureType.redirected,
  13. `Redirected when going from "${from.fullPath}" to "${stringifyRoute(
  14. to
  15. )}" via a navigation guard.`
  16. )
  17. }
  18. export function createNavigationDuplicatedError (from, to) {
  19. const error = createRouterError(
  20. from,
  21. to,
  22. NavigationFailureType.duplicated,
  23. `Avoided redundant navigation to current location: "${from.fullPath}".`
  24. )
  25. // backwards compatible with the first introduction of Errors
  26. error.name = 'NavigationDuplicated'
  27. return error
  28. }
  29. export function createNavigationCancelledError (from, to) {
  30. return createRouterError(
  31. from,
  32. to,
  33. NavigationFailureType.cancelled,
  34. `Navigation cancelled from "${from.fullPath}" to "${
  35. to.fullPath
  36. }" with a new navigation.`
  37. )
  38. }
  39. export function createNavigationAbortedError (from, to) {
  40. return createRouterError(
  41. from,
  42. to,
  43. NavigationFailureType.aborted,
  44. `Navigation aborted from "${from.fullPath}" to "${
  45. to.fullPath
  46. }" via a navigation guard.`
  47. )
  48. }
  49. function createRouterError (from, to, type, message) {
  50. const error = new Error(message)
  51. error._isRouter = true
  52. error.from = from
  53. error.to = to
  54. error.type = type
  55. return error
  56. }
  57. const propertiesToLog = ['params', 'query', 'hash']
  58. function stringifyRoute (to) {
  59. if (typeof to === 'string') return to
  60. if ('path' in to) return to.path
  61. const location = {}
  62. propertiesToLog.forEach(key => {
  63. if (key in to) location[key] = to[key]
  64. })
  65. return JSON.stringify(location, null, 2)
  66. }
  67. export function isError (err) {
  68. return Object.prototype.toString.call(err).indexOf('Error') > -1
  69. }
  70. export function isNavigationFailure (err, errorType) {
  71. return (
  72. isError(err) &&
  73. err._isRouter &&
  74. (errorType == null || err.type === errorType)
  75. )
  76. }