From fc5f0709dd79854d72c42fecf89271f35c05e0bb Mon Sep 17 00:00:00 2001 From: Josh Bowling Date: Tue, 28 May 2024 18:18:02 +0900 Subject: [PATCH] Update s3 error handling and logs Change-type: patch --- src/features/device-types/storage/s3.ts | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/features/device-types/storage/s3.ts b/src/features/device-types/storage/s3.ts index bcbf9e291..7b2df36ee 100644 --- a/src/features/device-types/storage/s3.ts +++ b/src/features/device-types/storage/s3.ts @@ -57,19 +57,20 @@ function createS3Client() { const s3Client = createS3Client(); -function logUnauthenticatedWarning( +function isUnauthenticatedError( clientS3: UnauthenticatedS3Facade | AWSWrapper.AWS.S3, - pathS3: string, err: any, -): void { - if ( +): boolean { + return ( clientS3 instanceof UnauthenticatedS3Facade && [401, 403].includes(err.statusCode) - ) { - console.warn( - `${err.code} (${err.statusCode}): ${pathS3} belongs to a private device type or has incorrect permissions`, - ); - } + ); +} + +function logUnauthenticated(pathS3: string, err: any): void { + console.warn( + `${err.code} (${err.statusCode}): ${pathS3} belongs to a private device type or has incorrect permissions`, + ); } async function getFileInfo(s3Path: string) { @@ -88,8 +89,12 @@ export async function getFile(s3Path: string) { }); return await req.promise(); } catch (err) { - // catch errors for private device types when running unauthenticated - logUnauthenticatedWarning(s3Client, s3Path, err); + if (isUnauthenticatedError(s3Client, err)) { + // catch errors for private device types when running unauthenticated + logUnauthenticated(s3Path, err); + } else { + throw err; + } } } @@ -148,9 +153,10 @@ export async function fileExists(s3Path: string): Promise { await getFileInfo(s3Path); return true; } catch (err) { - // catch errors for private device types when running unauthenticated - logUnauthenticatedWarning(s3Client, s3Path, err); - if (err.statusCode === 404) { + if (isUnauthenticatedError(s3Client, err)) { + // catch errors for private device types when running unauthenticated + logUnauthenticated(s3Path, err); + } else if (err.statusCode === 404) { return false; } throw err;