useErrorHandler.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useRouter } from 'vue-router'
  3. import { redirectToError } from '#shared/router/error.ts'
  4. import type { GraphQLHandlerError } from '#shared/types/error.ts'
  5. import { ErrorStatusCodes, GraphQLErrorTypes } from '#shared/types/error.ts'
  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. }