Skip to content

Commit

Permalink
Maintenance: Desktop view - Abort handling for Apollo client
Browse files Browse the repository at this point in the history
  • Loading branch information
vBenTec committed May 23, 2024
1 parent 0aa7644 commit aff7fb7
Showing 1 changed file with 50 additions and 44 deletions.
94 changes: 50 additions & 44 deletions app/frontend/shared/server/apollo/link/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,55 @@ import { GraphQLErrorTypes } from '#shared/types/error.ts'
import emitter from '#shared/utils/emitter.ts'
import log from '#shared/utils/log.ts'

const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
const errorContext = getErrorContext(operation)

const errorMessages: Array<string> = []

if (graphQLErrors) {
graphQLErrors.forEach(({ message, extensions, path }) => {
const { type, backtrace }: GraphQLErrorExtensionsHandler = {
type:
(extensions?.type as GraphQLErrorTypes) ||
GraphQLErrorTypes.NetworkError,
backtrace: extensions?.backtrace as string,
}

errorMessages.push(
`[GraphQL error - ${type}]: ${message}, Path: ${path}`,
backtrace,
)

if (
operation.operationName !== 'session' &&
type === GraphQLErrorTypes.NotAuthorized
) {
// Reset authenticated state after an unathenticated error type.
emitter.emit('sessionInvalid')

log.warn('Session invalid, trigger logout and show login page.')
}
})
}

if (networkError) {
// Suppress error message in Capybara test context, as it can happen if the
// test session is reset to 'about:blank' while requests are still running.
if (!VITE_TEST_MODE) errorMessages.push(`[Network error]: ${networkError}`)
// Network error implies application connection problems.
// TODO: what's missing here is a detection of web socket disconnects.
recordCommunicationFailure()
}

if (errorContext.logLevel === 'silent') return

log[errorContext.logLevel](...errorMessages)
})
const errorLink = onError(
({ graphQLErrors, networkError, operation, forward }) => {
const errorContext = getErrorContext(operation)

const errorMessages: Array<string> = []

// If the error is an AbortError, ignore it and forward the operation to avoid communication failure
if (networkError?.name === 'AbortError') return forward(operation)

if (graphQLErrors) {
graphQLErrors.forEach(({ message, extensions, path }) => {
const { type, backtrace }: GraphQLErrorExtensionsHandler = {
type:
(extensions?.type as GraphQLErrorTypes) ||
GraphQLErrorTypes.NetworkError,
backtrace: extensions?.backtrace as string,
}

errorMessages.push(
`[GraphQL error - ${type}]: ${message}, Path: ${path}`,
backtrace,
)

if (
operation.operationName !== 'session' &&
type === GraphQLErrorTypes.NotAuthorized
) {
// Reset authenticated state after an unathenticated error type.
emitter.emit('sessionInvalid')

log.warn('Session invalid, trigger logout and show login page.')
}
})
}

if (networkError) {
// Suppress error message in Capybara test context, as it can happen if the
// test session is reset to 'about:blank' while requests are still running.
if (!VITE_TEST_MODE)
errorMessages.push(`[Network error]: ${networkError}`)
// Network error implies application connection problems.
// TODO: what's missing here is a detection of web socket disconnects.
recordCommunicationFailure()
}

if (errorContext.logLevel === 'silent') return

log[errorContext.logLevel](...errorMessages)
},
)

export default errorLink

0 comments on commit aff7fb7

Please sign in to comment.