error.ts 1.1 KB

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