Skip to content

Commit

Permalink
Merge pull request #1646 from balena-io/joshbwlng/s3-errors
Browse files Browse the repository at this point in the history
Update s3 error handling and logs
  • Loading branch information
joshbwlng authored Jun 10, 2024
2 parents d3b61fa + fc5f070 commit d651c42
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/features/device-types/storage/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -148,9 +153,10 @@ export async function fileExists(s3Path: string): Promise<boolean> {
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;
Expand Down

0 comments on commit d651c42

Please sign in to comment.