error.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import { ErrorStatusCodes } from '#shared/types/error.ts'
  4. import type { NavigationHookAfter, Router } from 'vue-router'
  5. export enum ErrorRouteType {
  6. PublicError = 'Error',
  7. AuthenticatedError = 'ErrorTab',
  8. }
  9. export interface ErrorOptions {
  10. type?: ErrorRouteType
  11. title: string
  12. message: string
  13. statusCode: ErrorStatusCodes
  14. messagePlaceholder?: string[]
  15. route?: string
  16. }
  17. const defaultOptions: ErrorOptions = {
  18. title: __('Not Found'),
  19. message: __("This page doesn't exist."),
  20. messagePlaceholder: [],
  21. statusCode: ErrorStatusCodes.NotFound,
  22. }
  23. export const errorOptions = ref<ErrorOptions>({ ...defaultOptions })
  24. export const errorAfterGuard: NavigationHookAfter = (to) => {
  25. // we don't want to reset the error in case it was changed inside router hook
  26. // that way this hook will still fire, but we will keep changed options
  27. if (!to.query.redirect) {
  28. errorOptions.value = { ...defaultOptions }
  29. }
  30. }
  31. export const redirectErrorRoute = (options: Partial<ErrorOptions> = {}) => {
  32. errorOptions.value = {
  33. ...defaultOptions,
  34. ...options,
  35. }
  36. return {
  37. name: options.type ?? ErrorRouteType.PublicError,
  38. query: {
  39. redirect: '1',
  40. },
  41. }
  42. }
  43. export const redirectToError = (
  44. router: Router,
  45. options: Partial<ErrorOptions> = {},
  46. ) => router.replace(redirectErrorRoute(options))