error.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 interface ErrorOptions {
  6. title: string
  7. message: string
  8. statusCode: ErrorStatusCodes
  9. messagePlaceholder?: string[]
  10. route?: string
  11. }
  12. const defaultOptions: ErrorOptions = {
  13. title: __('Not Found'),
  14. message: __("This page doesn't exist."),
  15. messagePlaceholder: [],
  16. statusCode: ErrorStatusCodes.NotFound,
  17. }
  18. export const errorOptions = ref<ErrorOptions>({ ...defaultOptions })
  19. export const errorAfterGuard: NavigationHookAfter = (to) => {
  20. // we don't want to reset the error in case it was changed inside router hook
  21. // that way this hook will still fire, but we will keep changed options
  22. if (!to.query.redirect) {
  23. errorOptions.value = { ...defaultOptions }
  24. }
  25. }
  26. export const redirectToError = (
  27. router: Router,
  28. options: Partial<ErrorOptions> = {},
  29. ) => {
  30. errorOptions.value = {
  31. ...defaultOptions,
  32. ...options,
  33. }
  34. return router.replace({
  35. name: 'Error',
  36. query: {
  37. redirect: '1',
  38. },
  39. })
  40. }