Search code examples
node.jsamazon-s3aws-s3-client

@aws-sdk/client-s3, Why I got an unhandled error event when a step in pipeline throws an error?


I have a case when a transform stream throws an error in pipeline while the stream is handled by the method send of s3 client instance.

The error thrown is not caught by the try catch in the code.

I isolated the problem in a small example here: https://github.com/HalexV/throw-error-s3-upload.

The project uses a node version 18.

To run the example:

cd into the folder
nvm use
npm ci
docker compose up -d
npm start

I'm using async generators to create the readable and transform (https://nodejs.org/api/stream.html#streams-compatibility-with-async-generators-and-async-iterators).

If I change the uploadS3 to an async generator writable, the error is caught by the try catch.


Solution

  • While my question wasn't answered, I tried to debug my code and I found something interesting.

    At a certain moment I stopped at this code below on debugging: enter image description here

    Marked in red is the condition where the code was executed. This part of the code says that we are using a stream without a known length and recommends to use method Upload of the @aws-sdk/lib-storage.

    I found a stackoverflow's answer that helped me to use the method Upload instead of using the method send with PutObjectCommand. The previous way, the error wasn't caught correctly.

    So, consulting the @aws-sdk/lib-storage, I fix the code and the error was caught correctly. enter image description here enter image description here