Improve s3 exceptions logging

This commit is contained in:
Chocobozzz 2025-02-12 15:55:18 +01:00
parent 3116309914
commit bff5682f6c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 36 additions and 16 deletions

View File

@ -332,21 +332,25 @@ async function uploadToStorage (options: {
params: input
})
const response = await parallelUploads3.done()
// Check is needed even if the HTTP status code is 200 OK
// For more information, see https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html
if (!response.Bucket) {
const message = `Error uploading ${objectStorageKey} to bucket ${bucketInfo.BUCKET_NAME}`
logger.error(message, { response, ...lTags() })
throw new Error(message)
try {
const response = await parallelUploads3.done()
// Check is needed even if the HTTP status code is 200 OK
// For more information, see https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html
if (!response.Bucket) {
const message = `Error uploading ${objectStorageKey} to bucket ${bucketInfo.BUCKET_NAME}`
logger.error(message, { response, ...lTags() })
throw new Error(message)
}
logger.debug(
'Completed %s%s in bucket %s',
bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, { ...lTags(), responseMetadata: response.$metadata }
)
return getInternalUrl(bucketInfo, objectStorageKey)
} catch (err) {
throw parseS3Error(err)
}
logger.debug(
'Completed %s%s in bucket %s',
bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, { ...lTags(), reseponseMetadata: response.$metadata }
)
return getInternalUrl(bucketInfo, objectStorageKey)
}
async function applyOnPrefix (options: {
@ -395,3 +399,19 @@ function getACL (isPrivate: boolean) {
? CONFIG.OBJECT_STORAGE.UPLOAD_ACL.PRIVATE as ObjectCannedACL
: CONFIG.OBJECT_STORAGE.UPLOAD_ACL.PUBLIC as ObjectCannedACL
}
// Prevent logging too much information, in particular the body request
function parseS3Error (err: any) {
if (err.$response?.body) {
const body = err.$response.body
err.$response.body = {
rawHeaders: body.rawHeaders,
req: {
_header: body.req?._header
}
}
}
return err
}

View File

@ -266,7 +266,7 @@ app.use((_req, res: express.Response) => {
})
// Catch thrown errors
app.use((err, _req, res: express.Response, _next) => {
app.use((err, req, res: express.Response, _next) => {
// Format error to be logged
let error = 'Unknown error.'
if (err) {
@ -281,7 +281,7 @@ app.use((err, _req, res: express.Response, _next) => {
? (process as any)._getActiveRequests()
: undefined
logger.error('Error in controller.', { err: error, sql, activeRequests })
logger.error('Error in controller.', { err: error, sql, activeRequests, url: req.originalUrl })
return res.fail({
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,