Skip to content

Commit

Permalink
Refactor ApolloLoggerPlugin to skip logging for validation errors (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Tudor Stanciu <[email protected]>
  • Loading branch information
tstanciu and Tudor Stanciu authored May 23, 2024
1 parent 835974e commit bcad952
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-elephants-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@totalsoft/pino-apollo': patch
---

Refactor ApolloLoggerPlugin to skip message enrichment for business errors.
10 changes: 8 additions & 2 deletions packages/pino-apollo/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ export class ApolloLoggerPlugin implements ApolloServerPlugin<ApolloContextExten
messagePrefix: string,
{ request, operationName }: GraphQLRequestContext<ApolloContextExtension>,
extensions?: GraphQLErrorExtensions,
logger = this.logger
logger = this.logger,
skipEnrichment = false
) {
const message = `${messagePrefix} < ${getOperationName(request, operationName)} > \r\n`
const fullErrorMessage = `${message} ${error?.message} ${error?.stack} ${JSON.stringify(extensions)}`

if (!shouldSkipLogging(getOperationName(request, operationName))) logger.error(error, message)

if (skipEnrichment) {
if (this.securedMessages) error.stack = undefined
return
}
this._enrichError(error, message, fullErrorMessage)
}

Expand Down Expand Up @@ -107,9 +112,10 @@ export class ApolloLoggerPlugin implements ApolloServerPlugin<ApolloContextExten
},
didEncounterErrors: async (requestContext: GraphQLRequestContextDidEncounterErrors<ApolloContextExtension>) => {
for (const error of requestContext.errors) {
const shouldSkipEnrichment = error?.extensions?.code === 'BUSINESS_ERROR'
const message =
'[GraphQL_Execution][Error] The server encounters errors while parsing, validating, or executing the operation'
this._logRequestError(error, message, requestContext, error?.extensions, logger)
this._logRequestError(error, message, requestContext, error?.extensions, logger, shouldSkipEnrichment)
}
},
willSendResponse: async ({ operationName }: GraphQLRequestContextWillSendResponse<ApolloContextExtension>) => {
Expand Down

0 comments on commit bcad952

Please sign in to comment.