|
@@ -1,21 +1,16 @@
|
|
|
// Copyright (C) 2012-2021 Zammad Foundation, https://zammad-foundation.org/
|
|
|
|
|
|
-import log from '@common/utils/log'
|
|
|
import { ApolloError, OperationVariables } from '@apollo/client/core'
|
|
|
import {
|
|
|
BaseHandlerOptions,
|
|
|
CommonHandlerOptions,
|
|
|
CommonHandlerOptionsParameter,
|
|
|
- OperationFunction,
|
|
|
- OperationOptions,
|
|
|
OperationResult,
|
|
|
OperationReturn,
|
|
|
- OperationVariablesParameter,
|
|
|
} from '@common/types/server/apollo/handler'
|
|
|
import { Ref } from 'vue'
|
|
|
import useNotifications from '@common/composables/useNotifications'
|
|
|
import {
|
|
|
- GraphQLErrorExtensionsHandler,
|
|
|
GraphQLErrorReport,
|
|
|
GraphQLErrorTypes,
|
|
|
GraphQLHandlerError,
|
|
@@ -28,18 +23,8 @@ export default abstract class BaseHandler<
|
|
|
TResult,
|
|
|
TVariables
|
|
|
> = OperationReturn<TResult, TVariables>,
|
|
|
- TOperationFunction extends OperationFunction<
|
|
|
- TResult,
|
|
|
- TVariables
|
|
|
- > = OperationFunction<TResult, TVariables>,
|
|
|
- TOperationOptions extends OperationOptions<
|
|
|
- TResult,
|
|
|
- TVariables
|
|
|
- > = OperationOptions<TResult, TVariables>,
|
|
|
THandlerOptions = BaseHandlerOptions,
|
|
|
> {
|
|
|
- protected operation: TOperationFunction
|
|
|
-
|
|
|
public operationResult!: TOperationReturn
|
|
|
|
|
|
protected baseHandlerOptions: BaseHandlerOptions = {
|
|
@@ -47,32 +32,22 @@ export default abstract class BaseHandler<
|
|
|
errorNotitifactionMessage:
|
|
|
'An error occured during the operation. Please contact your administrator.',
|
|
|
errorNotitifactionType: 'error',
|
|
|
- errorLogLevel: 'error',
|
|
|
}
|
|
|
|
|
|
- protected handlerOptions!: CommonHandlerOptions<THandlerOptions>
|
|
|
-
|
|
|
- public error: Maybe<GraphQLHandlerError> = null
|
|
|
+ public handlerOptions!: CommonHandlerOptions<THandlerOptions>
|
|
|
|
|
|
constructor(
|
|
|
- operation: TOperationFunction,
|
|
|
- variables?: OperationVariablesParameter<TVariables>,
|
|
|
- options?: TOperationOptions,
|
|
|
+ operationResult: TOperationReturn,
|
|
|
handlerOptions?: CommonHandlerOptionsParameter<THandlerOptions>,
|
|
|
) {
|
|
|
- this.operation = operation
|
|
|
+ this.operationResult = operationResult
|
|
|
|
|
|
this.handlerOptions = this.mergedHandlerOptions(handlerOptions)
|
|
|
|
|
|
- this.execute(variables, options)
|
|
|
+ this.initialize()
|
|
|
}
|
|
|
|
|
|
- public execute(
|
|
|
- variables?: OperationVariablesParameter<TVariables>,
|
|
|
- options?: TOperationOptions,
|
|
|
- ): void {
|
|
|
- this.operationResult = this.operationExecute(variables, options)
|
|
|
-
|
|
|
+ protected initialize(): void {
|
|
|
this.operationResult.onError((error) => {
|
|
|
this.handleError(error)
|
|
|
})
|
|
@@ -86,61 +61,39 @@ export default abstract class BaseHandler<
|
|
|
return this.operationResult.error
|
|
|
}
|
|
|
|
|
|
- protected abstract operationExecute(
|
|
|
- variables?: OperationVariablesParameter<TVariables>,
|
|
|
- options?: TOperationOptions,
|
|
|
- ): TOperationReturn
|
|
|
-
|
|
|
protected handleError(error: ApolloError): void {
|
|
|
const options = this.handlerOptions
|
|
|
|
|
|
if (options.errorShowNotification) {
|
|
|
- const notification = useNotifications()
|
|
|
+ const { notify } = useNotifications()
|
|
|
|
|
|
- notification.notify({
|
|
|
+ notify({
|
|
|
message: options.errorNotitifactionMessage,
|
|
|
type: options.errorNotitifactionType,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- // TODO: Maybe it's also a option to use the error-Link from the apollo client for the general error output.
|
|
|
- // Downside would be, that the error messages can not be avoided for a single query/mutation if i saw it correctly (but this is maybe
|
|
|
- // only needed for some special things, like the first session id check).
|
|
|
- const errorMessagePrefix = '[GraphQLClient]'
|
|
|
- const { graphQLErrors, networkError } = error
|
|
|
-
|
|
|
- if (graphQLErrors.length > 0) {
|
|
|
- const { message, extensions }: GraphQLErrorReport = graphQLErrors[0]
|
|
|
- const { type, backtrace }: GraphQLErrorExtensionsHandler = {
|
|
|
- type: extensions?.type || GraphQLErrorTypes.NetworkError,
|
|
|
- backtrace: extensions?.backtrace,
|
|
|
+ if (options.errorCallback) {
|
|
|
+ const { graphQLErrors, networkError } = error
|
|
|
+ let errorHandler: GraphQLHandlerError
|
|
|
+
|
|
|
+ if (graphQLErrors.length > 0) {
|
|
|
+ const { message, extensions }: GraphQLErrorReport = graphQLErrors[0]
|
|
|
+ errorHandler = {
|
|
|
+ type: extensions?.type || GraphQLErrorTypes.NetworkError,
|
|
|
+ message,
|
|
|
+ }
|
|
|
+ } else if (networkError) {
|
|
|
+ errorHandler = {
|
|
|
+ type: GraphQLErrorTypes.NetworkError,
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ errorHandler = {
|
|
|
+ type: GraphQLErrorTypes.UnkownError,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- this.error = {
|
|
|
- message: `${errorMessagePrefix} GraphQL error - ${message}`,
|
|
|
- type,
|
|
|
- backtrace,
|
|
|
- }
|
|
|
- } else if (networkError) {
|
|
|
- this.error = {
|
|
|
- type: GraphQLErrorTypes.NetworkError,
|
|
|
- message: `${errorMessagePrefix} Network error - ${networkError}`,
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.error = {
|
|
|
- type: GraphQLErrorTypes.UnkownError,
|
|
|
- message: 'Unknown error.',
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (options.errorLogLevel !== 'silent') {
|
|
|
- const errorMessages: Array<string> = [this.error.message]
|
|
|
-
|
|
|
- if (this.error.backtrace) {
|
|
|
- errorMessages.push(this.error.backtrace)
|
|
|
- }
|
|
|
-
|
|
|
- log[options.errorLogLevel](...errorMessages)
|
|
|
+ options.errorCallback(errorHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -153,13 +106,4 @@ export default abstract class BaseHandler<
|
|
|
handlerOptions,
|
|
|
) as CommonHandlerOptions<THandlerOptions>
|
|
|
}
|
|
|
-
|
|
|
- public abstract onLoaded(): Promise<Maybe<TResult>>
|
|
|
-
|
|
|
- // TODO: Maybe we can add a pick func
|
|
|
- public loadedResult(): Promise<Maybe<TResult>> {
|
|
|
- return this.onLoaded()
|
|
|
- .then((data: Maybe<TResult>) => data)
|
|
|
- .catch(() => null)
|
|
|
- }
|
|
|
}
|