useErrorHandler.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { redirectToError } from '#shared/router/error.ts'
  3. import type { GraphQLHandlerError } from '#shared/types/error.ts'
  4. import { ErrorStatusCodes, GraphQLErrorTypes } from '#shared/types/error.ts'
  5. import { useRouter } from 'vue-router'
  6. interface ErrorMessages {
  7. notFound?: string
  8. forbidden?: string
  9. }
  10. export const useErrorHandler = () => {
  11. const router = useRouter()
  12. const createQueryErrorHandler = (messages: ErrorMessages) => {
  13. return (errorHandler: GraphQLHandlerError) => {
  14. let title: string
  15. let message: string | undefined
  16. let messagePlaceholder: string[] = []
  17. let statusCode: number
  18. if (errorHandler.type === GraphQLErrorTypes.RecordNotFound) {
  19. title = __('Not found')
  20. message = messages.notFound
  21. statusCode = ErrorStatusCodes.NotFound
  22. } else if (errorHandler.type === GraphQLErrorTypes.Forbidden) {
  23. title = __('Forbidden')
  24. message = messages.forbidden
  25. statusCode = ErrorStatusCodes.Forbidden
  26. } else if (errorHandler.type !== GraphQLErrorTypes.NotAuthorized) {
  27. title = __('Internal Error')
  28. message = errorHandler.message
  29. ? __('Something went wrong. Received message: %s')
  30. : __('Something went wrong.')
  31. messagePlaceholder = errorHandler.message ? [errorHandler.message] : []
  32. statusCode = ErrorStatusCodes.InternalError
  33. } else {
  34. return true
  35. }
  36. redirectToError(router, {
  37. title,
  38. message,
  39. messagePlaceholder,
  40. statusCode,
  41. })
  42. // don't show the notification
  43. return false
  44. }
  45. }
  46. return {
  47. createQueryErrorHandler,
  48. }
  49. }