smithy-lang / smithy-typescript

Smithy code generators for TypeScript. (in development)
Apache License 2.0
211 stars 78 forks source link

400: UnknownError at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20) #1292

Closed AllanOricil closed 1 month ago

AllanOricil commented 1 month ago

AWS SDK V3 S3 Module is throwing an exception when executing HeadBucketCommand

image image

In the next images, you can see that the message bucket ${s3BucketName} exists? ${bucketExists} isn't displayed proving that the exeception happens right when calling HeadBucketCommand. Since the error that was thrown isn't of type NotFound, my method let the exception propagate to stop execution. However, because the error is unknown I can't figure out what it is. The only clue I have is that it comes from @smithy/smithy-client

image image

This issue wasn't happening 2 weeks ago

AllanOricil commented 1 month ago

Discovered that when the bucket doesn't exist, the error is Unknown

https://github.com/rusoto/rusoto/issues/1099

which isn't documented in the sdk doc

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/HeadBucketCommand/

Apparently someone reported it already, but it was never fixed. The error.name is also not set as "NoFound". The following code doesn't work either.

async bucketExists(bucketName: string): Promise<boolean> {
    try {
      const headBucketCommand = new HeadBucketCommand({
        Bucket: bucketName,
      });
      await this.client.send(headBucketCommand);
      // NOTE: if no error is thrown, bucket exists
      return true;
    } catch (error) {
      // NOTE: if err code is NotFound, bucket doesn't exist
      if ((error as Error).name === "NotFound") {
        return false;
      }

      throw error;
    }
  }
AllanOricil commented 1 month ago

Just came back to say that I found the real root cause of this issue. This error actually occurs if you pass an invalid bucket name.

The following code works, contrary to what I said previously.

async bucketExists(bucketName: string): Promise<boolean> {
    try {
      const headBucketCommand = new HeadBucketCommand({
        Bucket: bucketName,
      });
      await this.client.send(headBucketCommand);
      // NOTE: if no error is thrown, bucket exists
      return true;
    } catch (error) {
      // NOTE: if err code is NotFound, bucket doesn't exist
      if ((error as Error).name === "NotFound") {
        return false;
      }

      throw error;
    }
  }