|
| 1 | +import Vue from 'vue'; |
| 2 | +import { |
| 3 | + ApolloError, |
| 4 | + ApolloErrorType, |
| 5 | + ApolloOperationContext, |
| 6 | + GraphQLError, |
| 7 | + InputValidationError, |
| 8 | + NetworkError, |
| 9 | + ProcessedApolloError, |
| 10 | + ServerError, |
| 11 | + UnauthorizedError, |
| 12 | +} from './types'; |
| 13 | + |
| 14 | +export function isApolloError(error: ApolloError | any): error is ApolloError { |
| 15 | + return error.graphQLErrors !== undefined; |
| 16 | +} |
| 17 | + |
| 18 | +export function isGraphQLError(error: GraphQLError | any): error is GraphQLError { |
| 19 | + return error.extensions !== undefined; |
| 20 | +} |
| 21 | + |
| 22 | +function normalizeErrorMessage(message: GraphQLError['message']): string { |
| 23 | + if (typeof message === 'object') { |
| 24 | + if (message.error != null) { |
| 25 | + return message.error; |
| 26 | + } |
| 27 | + |
| 28 | + return 'Unknown error: ' + JSON.stringify(message); |
| 29 | + } |
| 30 | + |
| 31 | + return message; |
| 32 | +} |
| 33 | + |
| 34 | +function normalizeError(error: GraphQLError | Error): Error { |
| 35 | + if (isGraphQLError(error)) { |
| 36 | + return { |
| 37 | + ...error, |
| 38 | + message: normalizeErrorMessage(error.message), |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + return error; |
| 43 | +} |
| 44 | + |
| 45 | +function translateErrorMessage(messageOrCode: string, translations: Record<string, string>): string { |
| 46 | + return translations[messageOrCode] ?? messageOrCode; |
| 47 | +} |
| 48 | + |
| 49 | +export interface ApolloErrorProcessorOptions<TApp = Vue, TContext = ApolloOperationContext> { |
| 50 | + app?: TApp; |
| 51 | + context?: TContext; |
| 52 | + isUnauthorizedError?: (error: GraphQLError) => boolean; |
| 53 | + onUnauthorizedError?: (error: UnauthorizedError) => boolean | void; |
| 54 | + onInputValidationError?: (error: InputValidationError) => boolean | void; |
| 55 | + onServerError?: (error: ServerError) => boolean | void; |
| 56 | + onNetworkError?: (error: NetworkError) => boolean | void; |
| 57 | + translations?: Record<string, string>; |
| 58 | +} |
| 59 | + |
| 60 | +export const defaultErrorMessageTranslations: Record<string, string> = { |
| 61 | + FAILED_TO_FETCH: |
| 62 | + 'Unable to communicate with server. The service may be down or you may be offline. Try again in a moment.', |
| 63 | + INTERNAL_SERVER_ERROR: `A server error has occurred.`, |
| 64 | +}; |
| 65 | + |
| 66 | +const defaultProcessorOptions: ApolloErrorProcessorOptions = { |
| 67 | + isUnauthorizedError: (error: GraphQLError) => |
| 68 | + error.message === 'Unauthorized' || |
| 69 | + error.extensions?.code === 'FORBIDDEN' || |
| 70 | + error.extensions?.exception?.status === 401, |
| 71 | + translations: defaultErrorMessageTranslations, |
| 72 | +}; |
| 73 | + |
| 74 | +export interface ApolloErrorProcessorResult { |
| 75 | + unhandledErrors: ProcessedApolloError[]; |
| 76 | + handledErrors: ProcessedApolloError[]; |
| 77 | +} |
| 78 | + |
| 79 | +function processGraphQLError( |
| 80 | + error: GraphQLError, |
| 81 | + options: ApolloErrorProcessorOptions = {}, |
| 82 | +): ApolloErrorProcessorResult { |
| 83 | + options = { ...defaultProcessorOptions, ...options }; |
| 84 | + const { isUnauthorizedError, onUnauthorizedError, onInputValidationError, onServerError, translations } = options; |
| 85 | + |
| 86 | + if (isUnauthorizedError != null && isUnauthorizedError(error)) { |
| 87 | + // Unauthorized (not logged in, or not allowed) error |
| 88 | + const processedError: UnauthorizedError = { |
| 89 | + type: ApolloErrorType.UNAUTHORIZED_ERROR, |
| 90 | + error: normalizeError(error), |
| 91 | + message: normalizeErrorMessage(error.message), |
| 92 | + path: error.path, |
| 93 | + }; |
| 94 | + |
| 95 | + if (onUnauthorizedError != null && onUnauthorizedError(processedError)) { |
| 96 | + return { unhandledErrors: [], handledErrors: [processedError] }; |
| 97 | + } |
| 98 | + |
| 99 | + return { unhandledErrors: [processedError], handledErrors: [] }; |
| 100 | + } |
| 101 | + |
| 102 | + if (error.extensions?.validationErrors != null) { |
| 103 | + // User input validation error |
| 104 | + const processedError: InputValidationError = { |
| 105 | + type: ApolloErrorType.INPUT_VALIDATION_ERROR, |
| 106 | + error: normalizeError(error), |
| 107 | + message: normalizeErrorMessage(error.message), |
| 108 | + path: error.path, |
| 109 | + invalidArgs: error.extensions.invalidArgs, |
| 110 | + violations: error.extensions.validationErrors, |
| 111 | + }; |
| 112 | + |
| 113 | + if (onInputValidationError != null && onInputValidationError(processedError)) { |
| 114 | + return { unhandledErrors: [], handledErrors: [processedError] }; |
| 115 | + } |
| 116 | + |
| 117 | + return { unhandledErrors: [processedError], handledErrors: [] }; |
| 118 | + } |
| 119 | + |
| 120 | + // Other GraphQL resolver error - probably a bug |
| 121 | + const processedError: ServerError = { |
| 122 | + type: error.extensions?.code != null ? error.extensions.code : ApolloErrorType.SERVER_ERROR, |
| 123 | + error: normalizeError(error), |
| 124 | + message: translateErrorMessage('INTERNAL_SERVER_ERROR', translations ?? defaultErrorMessageTranslations), |
| 125 | + path: error.path, |
| 126 | + }; |
| 127 | + |
| 128 | + if (onServerError != null && onServerError(processedError)) { |
| 129 | + return { unhandledErrors: [], handledErrors: [processedError] }; |
| 130 | + } |
| 131 | + |
| 132 | + return { unhandledErrors: [processedError], handledErrors: [] }; |
| 133 | +} |
| 134 | + |
| 135 | +function processNetworkError( |
| 136 | + error: ApolloError, |
| 137 | + options: ApolloErrorProcessorOptions = {}, |
| 138 | +): ApolloErrorProcessorResult { |
| 139 | + options = { ...defaultProcessorOptions, ...options }; |
| 140 | + const { onNetworkError, translations } = options; |
| 141 | + |
| 142 | + let message: string = |
| 143 | + error.networkError != null && error.networkError.message != null |
| 144 | + ? normalizeErrorMessage(error.networkError.message) |
| 145 | + : normalizeErrorMessage(error.message); |
| 146 | + |
| 147 | + switch (message) { |
| 148 | + case 'Failed to fetch': |
| 149 | + message = translateErrorMessage('FAILED_TO_FETCH', translations ?? defaultErrorMessageTranslations); |
| 150 | + break; |
| 151 | + } |
| 152 | + |
| 153 | + const processedError: NetworkError = { |
| 154 | + type: ApolloErrorType.NETWORK_ERROR, |
| 155 | + error, |
| 156 | + statusCode: error.networkError != null ? error.networkError.statusCode : undefined, |
| 157 | + message, |
| 158 | + }; |
| 159 | + |
| 160 | + if (onNetworkError != null && onNetworkError(processedError)) { |
| 161 | + return { unhandledErrors: [], handledErrors: [processedError] }; |
| 162 | + } |
| 163 | + |
| 164 | + return { unhandledErrors: [processedError], handledErrors: [processedError] }; |
| 165 | +} |
| 166 | + |
| 167 | +export function processApolloError( |
| 168 | + error: ApolloError, |
| 169 | + options: ApolloErrorProcessorOptions = {}, |
| 170 | +): ApolloErrorProcessorResult { |
| 171 | + options = { ...defaultProcessorOptions, ...options }; |
| 172 | + const { onServerError } = options; |
| 173 | + |
| 174 | + if (error.graphQLErrors != null && error.graphQLErrors.length > 0) { |
| 175 | + // Successful request but with errors from the resolver |
| 176 | + const errorProcessorResults = error.graphQLErrors.map(graphQLError => processGraphQLError(graphQLError, options)); |
| 177 | + |
| 178 | + return { |
| 179 | + unhandledErrors: errorProcessorResults.flatMap(result => result.unhandledErrors), |
| 180 | + handledErrors: errorProcessorResults.flatMap(result => result.handledErrors), |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + if (error.networkError?.result?.errors != null && error.networkError.result.errors.length > 0) { |
| 185 | + // Network error that contains GraphQL errors inside it. Can occur when server responds with a non-200 status code |
| 186 | + const errorProcessorResults = error.networkError.result.errors.map(graphQLError => |
| 187 | + processGraphQLError(graphQLError, options), |
| 188 | + ); |
| 189 | + |
| 190 | + return { |
| 191 | + unhandledErrors: errorProcessorResults.flatMap(result => result.unhandledErrors), |
| 192 | + handledErrors: errorProcessorResults.flatMap(result => result.handledErrors), |
| 193 | + }; |
| 194 | + } |
| 195 | + |
| 196 | + if (error.networkError != null) { |
| 197 | + // Network error, e.g. server is not responding or some other exception occurs |
| 198 | + return processNetworkError(error, options); |
| 199 | + } |
| 200 | + |
| 201 | + // Some other internal server error |
| 202 | + const processedError: ServerError = { |
| 203 | + type: ApolloErrorType.SERVER_ERROR, |
| 204 | + error, |
| 205 | + message: error.message, |
| 206 | + }; |
| 207 | + |
| 208 | + if (onServerError != null && onServerError(processedError)) { |
| 209 | + return { unhandledErrors: [], handledErrors: [processedError] }; |
| 210 | + } |
| 211 | + |
| 212 | + return { unhandledErrors: [processedError], handledErrors: [] }; |
| 213 | +} |
0 commit comments