BaseHandler.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. useNotifications,
  4. NotificationTypes,
  5. } from '#shared/components/CommonNotifications/index.ts'
  6. import type { GraphQLHandlerError } from '#shared/types/error.ts'
  7. import { GraphQLErrorTypes } from '#shared/types/error.ts'
  8. import type {
  9. BaseHandlerOptions,
  10. CommonHandlerOptions,
  11. CommonHandlerOptionsParameter,
  12. OperationResult,
  13. OperationReturn,
  14. } from '#shared/types/server/apollo/handler.ts'
  15. import getUuid from '#shared/utils/getUuid.ts'
  16. import type { ApolloError, OperationVariables } from '@apollo/client/core'
  17. import type { GraphQLFormattedError } from 'graphql'
  18. import type { Ref } from 'vue'
  19. export default abstract class BaseHandler<
  20. TResult = OperationResult,
  21. TVariables extends OperationVariables = OperationVariables,
  22. TOperationReturn extends OperationReturn<
  23. TResult,
  24. TVariables
  25. > = OperationReturn<TResult, TVariables>,
  26. THandlerOptions = BaseHandlerOptions,
  27. > {
  28. public operationResult!: TOperationReturn
  29. protected baseHandlerOptions: BaseHandlerOptions = {
  30. errorShowNotification: true,
  31. errorNotificationMessage: '',
  32. errorNotificationType: NotificationTypes.Error,
  33. }
  34. public handlerOptions!: CommonHandlerOptions<THandlerOptions>
  35. protected handlerId: string
  36. constructor(
  37. operationResult: TOperationReturn,
  38. handlerOptions?: CommonHandlerOptionsParameter<THandlerOptions>,
  39. ) {
  40. this.operationResult = operationResult
  41. this.handlerOptions = this.mergedHandlerOptions(handlerOptions)
  42. this.handlerId = getUuid()
  43. this.initialize()
  44. }
  45. protected initialize(): void {
  46. this.operationResult.onError((error) => {
  47. this.handleError(error)
  48. })
  49. }
  50. public loading(): Ref<boolean> {
  51. return this.operationResult.loading
  52. }
  53. public operationError(): Ref<Maybe<ApolloError>> {
  54. return this.operationResult.error
  55. }
  56. public onError(callback: (error: ApolloError) => void): void {
  57. this.operationResult.onError(callback)
  58. }
  59. protected handleError(error: ApolloError): void {
  60. const options = this.handlerOptions
  61. let triggerNotification = options.errorShowNotification
  62. const { graphQLErrors, networkError } = error
  63. let errorHandler: GraphQLHandlerError
  64. if (graphQLErrors.length > 0) {
  65. const { message, extensions }: GraphQLFormattedError = graphQLErrors[0]
  66. let type =
  67. (extensions?.type as GraphQLErrorTypes) ||
  68. GraphQLErrorTypes.UnknownError
  69. // When it's not a known type, use the unknown error type.
  70. if (!Object.values(GraphQLErrorTypes).includes(type)) {
  71. type = GraphQLErrorTypes.UnknownError
  72. }
  73. errorHandler = {
  74. type,
  75. message,
  76. }
  77. } else if (networkError) {
  78. errorHandler = {
  79. type: GraphQLErrorTypes.NetworkError,
  80. }
  81. } else {
  82. errorHandler = {
  83. type: GraphQLErrorTypes.UnknownError,
  84. }
  85. }
  86. if (errorHandler.type === GraphQLErrorTypes.NotAuthorized) {
  87. triggerNotification = false
  88. }
  89. if (options.errorCallback) {
  90. const trigger = options.errorCallback(errorHandler)
  91. if (typeof trigger === 'boolean') {
  92. triggerNotification = trigger
  93. }
  94. }
  95. if (triggerNotification) {
  96. // TODO enable and fix all tests
  97. // if (import.meta.env.DEV) {
  98. // console.error(error)
  99. // }
  100. useNotifications().notify({
  101. id: this.handlerId,
  102. message: this.errorNotificationMessage(
  103. errorHandler.type,
  104. errorHandler.message,
  105. ),
  106. type: options.errorNotificationType,
  107. })
  108. }
  109. }
  110. protected mergedHandlerOptions(
  111. handlerOptions?: CommonHandlerOptionsParameter<THandlerOptions>,
  112. ): CommonHandlerOptions<THandlerOptions> {
  113. // The merged type is always safe as a 'CommonHandlerOptions<THandlerOptions>' type.
  114. return Object.assign(
  115. this.baseHandlerOptions,
  116. handlerOptions,
  117. ) as CommonHandlerOptions<THandlerOptions>
  118. }
  119. private errorNotificationMessage(
  120. errorType: GraphQLErrorTypes,
  121. errorMessage?: string,
  122. ): string {
  123. const defaultErrorNotificationMessage = __(
  124. 'An error occured during the operation.',
  125. )
  126. const fallbackErrorMessage =
  127. errorType === GraphQLErrorTypes.UnknownError || !errorMessage
  128. ? defaultErrorNotificationMessage
  129. : errorMessage
  130. return this.handlerOptions.errorNotificationMessage || fallbackErrorMessage
  131. }
  132. }