Skip to content

datastore-s3 throws the wrong error when a file isn't found on s3 #331

@azf20

Description

@azf20

When using datastore-s3, calling helia.pins.add or helia.pins.ls fails with a "NoSuchKey" error

err: {
      "type": "GetFailedError",
      "message": "NoSuchKey: The specified key does not exist.",

If a file isn't found a "NotFoundError" is gracefully handled, but a GetFailedError throws. datastore-s3's "get" method currently throws a GetFailedError when a given file isn't found.

This is because the 404 status check doesn't line up with the response from s3 (this line)

Applied the following patch to fix:

diff --git a/dist/src/index.js b/dist/src/index.js
index 3dc550c3fa0abaf3c6c1fe7bb7ca4107c824ba3d..88f43d0939f9dbc754097723c0bcab15fdacae88 100644
--- a/dist/src/index.js
+++ b/dist/src/index.js
@@ -113,7 +113,7 @@ export class S3Datastore extends BaseDatastore {
             return await toBuffer(data.Body);
         }
         catch (err) {
-            if (err.statusCode === 404) {
+            if (err.statusCode === 404 || err.$metadata.httpStatusCode === 404 || String(err).includes('NoSuchKey')) {
                 throw new NotFoundError(String(err));
             }
             throw new GetFailedError(String(err));

datastore-fs error handling for reference

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/triageNeeds initial labeling and prioritization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions