Skip to content

Commit

Permalink
fix(virus-scanner): rework error logging in pino (#7128)
Browse files Browse the repository at this point in the history
Pino has in-built serialisers for logging Errors, but only on Errors
or Errors in nested `err` properties. Change logging accordingly.
  • Loading branch information
LoneRifle committed Mar 8, 2024
1 parent da0d5b7 commit d68b84a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions serverless/virus-scanner/src/__tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('handler', () => {
expect(mockLoggerWarn).toHaveBeenCalledWith(
expect.objectContaining({
message: 'File not found',
error: new Error('File not found'),
err: new Error('File not found'),
quarantineFileKey: mockUUID,
}),
)
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('handler', () => {
expect(mockLoggerError).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to scan file',
error: new Error('Failed to scan file'),
err: new Error('Failed to scan file'),
quarantineFileKey: mockUUID,
}),
)
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('handler', () => {
expect(mockLoggerError).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to move file to clean bucket',
error: new Error('Failed to move file'),
err: new Error('Failed to move file'),
bucket: 'local-virus-scanner-quarantine-bucket',
key: mockUUID,
}),
Expand Down
4 changes: 2 additions & 2 deletions serverless/virus-scanner/src/__tests/s3.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('S3Service', () => {
expect.objectContaining({
bucketName: 'bucketName',
objectKey: 'objectKey',
error: new Error('Body is empty'),
err: new Error('Body is empty'),
}),
'Failed to get object from s3',
)
Expand All @@ -172,7 +172,7 @@ describe('S3Service', () => {
expect.objectContaining({
bucketName: 'bucketName',
objectKey: 'objectKey',
error: new Error('VersionId is empty'),
err: new Error('VersionId is empty'),
}),
'Failed to get object from s3',
)
Expand Down
16 changes: 8 additions & 8 deletions serverless/virus-scanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export const handler = async (
bucketName: quarantineBucket,
objectKey: quarantineFileKey,
})
} catch (error) {
} catch (err) {
logger.warn({
message: 'File not found',
error,
err,
quarantineFileKey,
})
return {
Expand All @@ -88,10 +88,10 @@ export const handler = async (
let scanResult
try {
scanResult = await scanFileStream(body)
} catch (error) {
} catch (err) {
logger.error({
message: 'Failed to scan file',
error,
err,
quarantineFileKey,
})
return {
Expand Down Expand Up @@ -122,11 +122,11 @@ export const handler = async (
objectKey: quarantineFileKey,
versionId,
})
} catch (error) {
} catch (err) {
// Log but do not halt execution as we still want to return 400 for malicious file
logger.error({
message: 'Failed to delete file from quarantine bucket',
error,
err,
key: quarantineFileKey,
})
}
Expand Down Expand Up @@ -158,10 +158,10 @@ export const handler = async (
destinationBucketName: cleanBucket,
destinationObjectKey: cleanFileKey,
})
} catch (error) {
} catch (err) {
logger.error({
message: 'Failed to move file to clean bucket',
error,
err,
bucket: quarantineBucket,
key: quarantineFileKey,
})
Expand Down
18 changes: 9 additions & 9 deletions serverless/virus-scanner/src/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ export class S3Service {
)

return { body, versionId } as GetS3FileStreamResult
} catch (error) {
} catch (err) {
this.logger.error(
{
bucketName,
objectKey,
error,
err,
},
'Failed to get object from s3',
)

throw error
throw err
}
}

Expand Down Expand Up @@ -112,17 +112,17 @@ export class S3Service {
},
'Deleted document from s3',
)
} catch (error) {
} catch (err) {
this.logger.error(
{
bucketName,
objectKey,
error,
err,
},
'Failed to delete object from s3',
)

throw error
throw err
}
}

Expand Down Expand Up @@ -189,20 +189,20 @@ export class S3Service {
)

return VersionId
} catch (error) {
} catch (err) {
this.logger.error(
{
sourceBucketName,
sourceObjectKey,
sourceObjectVersionId,
destinationBucketName,
destinationObjectKey,
error,
err,
},
'Failed to move object in s3',
)

throw error
throw err
}
}
}

0 comments on commit d68b84a

Please sign in to comment.