Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s3-client generates uncatchable error on bad Body type #4848

Closed
3 tasks done
rafak opened this issue Jun 16, 2023 · 7 comments · Fixed by #4880
Closed
3 tasks done

s3-client generates uncatchable error on bad Body type #4848

rafak opened this issue Jun 16, 2023 · 7 comments · Fixed by #4880
Assignees
Labels
bug This issue is a bug. p2 This is a standard priority issue queued This issues is on the AWS team's backlog

Comments

@rafak
Copy link

rafak commented Jun 16, 2023

Checkboxes for prior research

Describe the bug

when using client-s3/PutObjectCommand with a bad Body type, an uncatchable TypeError is thrown

SDK version number

@aws-sdk/client-s3: ^3.352.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v16.20.0

Reproduction Steps

dependencies: @aws-sdk/client-s3 version ^3.352.0
running the following inside a docker container:

file x.js

'use strict'

process.on('uncaughtException', x => console.log('Uncaught Exception:', x))
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')
let config = {
  credentials: {
    accessKeyId: 'someAccessKeyId',
    secretAccessKey: 'someSecretAccessKey'
  }
}

const send = async () => {
  try {
    let s = new S3Client(config)
    let c = new PutObjectCommand({
      Bucket:'test-bucket', Key: 'testing-sdk', Body: {}
    })
    let result = await s.send(c)
    return result
  } catch (err) {
    console.error('GOT ERROR', err)
    throw err
  }
}
send()
  .then(result => console.log('RESULT', result))
  .catch(err => console.error('ERROR in promise chain:', err))

execute:

root@6c54747fe69a:/app# node x.js 
Uncaught Exception: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at writeBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:40:32)
    at writeRequestBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:31:9)
    at async resolve (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:55:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Observed Behavior

using and empty object ({}) as the input parameter Body to the PutObjectCommand creates and uncaught exception.
the error was not caught in the try-catch block
the exception was caught on the process level:
process.on('uncaughtException', (...))

Uncaught Exception: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at writeBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:40:32)
    at writeRequestBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:31:9)
    at async resolve (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:55:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Expected Behavior

i expected the aws-sdk to validate the input and throw an error early
i expected the error to be caught in the catch (err) {} block of the async send() {} function

Possible Solution

No response

Additional Information/Context

No response

@rafak rafak added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 16, 2023
@rafak rafak changed the title s3-client generates unhandled rejection / uncatchable error on bad Body type s3-client generates uncatchable error on bad Body type Jun 16, 2023
@RanVaknin RanVaknin self-assigned this Jun 19, 2023
@RanVaknin
Copy link
Contributor

RanVaknin commented Jun 19, 2023

Hi @rafak ,

Unfortunately I'm not able to reproduce the reported behavior. My application logs the error from the context of the catch block:

const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')

const send = async () => {
  try {
    const client = new S3Client({
        region: "us-east-1", 
        credentials: {
		"accessKeyId": "Foo",
		"secretAccessKey": "Bar",
	}})
    const command = new PutObjectCommand({
      Bucket:'testbucket-3650', Key: 'testing-sdk', Body: {}
    })
    const result = await client.send(command)
    return result
  } catch (err) {
    console.error('GOT ERROR', err)
    throw err
  }
}
send()

Dockerfile:

FROM node:16.20.0
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "sample.js"]
$ docker build -t 4848 . 
$ docker run -it --rm 4848

node:internal/errors:478
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at writeBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:40:32)
    at writeRequestBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:31:9)
    at async resolve (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:55:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

I noticed that you are using both async await and .then().catch() which is the syntax for promises. While I'm not sure if this is the source of the issue, I would just choose one or the other. See my code example for how I would write make this call.

If you can give my implementation a try and let me know how it goes. Otherwise can you provide a github repository with minimal code required to consistently reproduce this issue?

Thank you very much,
Ran~

@RanVaknin RanVaknin added response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jun 19, 2023
@rafak
Copy link
Author

rafak commented Jun 20, 2023

@RanVaknin

My application logs the error from the context of the catch block

i see the error but i don't see the "GOT ERROR" from the console.error line, which confirms that the error was uncaught.
please note that my code example had a process.on('uncaughtException') where the error was handled. your snippet is missing this event handler.

the await/.then does not matter here, you are calling send() but not handling anything thrown by the function (notice that send() will rethrow the caught error and it will remain unhandled) - i could use await but it would have to be wrapped in a function call

(async () => { try { await send() } catch (err) { console.log('do something with err....', err)}})()

but that does not change much

anyway based on your code i would expect your snippet to produce output along the lines:

GOT ERROR TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at writeBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:40:32)
    at writeRequestBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:31:9)
    at async resolve (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:55:17) {
  code: 'ERR_INVALID_ARG_TYPE'

@rafak
Copy link
Author

rafak commented Jun 20, 2023

@RanVaknin

how about if you swallow the error?

const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')

const send = async () => {
  try {
    const client = new S3Client({
        region: "us-east-1", 
        credentials: {
		"accessKeyId": "Foo",
		"secretAccessKey": "Bar",
	}})
    const command = new PutObjectCommand({
      Bucket:'testbucket-3650', Key: 'testing-sdk', Body: {}
    })
    const result = await client.send(command)
    return result
  } catch (err) {
    // swallow error
  }
}
send()

will there be any output on the console?

@rafak
Copy link
Author

rafak commented Jun 20, 2023

@RanVaknin
this is what i get after running the above test:

the file (x.js):

root@19ae8a71a36f:/app# cat x.js
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')

const send = async () => {
  try {
    const client = new S3Client({
        region: "us-east-1", 
        credentials: {
		"accessKeyId": "Foo",
		"secretAccessKey": "Bar",
	}})
    const command = new PutObjectCommand({
      Bucket:'testbucket-3650', Key: 'testing-sdk', Body: {}
    })
    const result = await client.send(command)
    return result
  } catch (err) {
    // swallow error
  }
}
send()
root@19ae8a71a36f:/app# 

execution:

root@19ae8a71a36f:/app# node x.js 
node:internal/errors:478
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:387:5)
    at Function.from (node:buffer:328:9)
    at writeBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:40:32)
    at writeRequestBody (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/write-request-body.js:31:9)
    at async resolve (/app/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:55:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

would you agree that the error should not be output to the console if it was caught by the try-catch block in the send() function?

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Jun 21, 2023
@RanVaknin RanVaknin added the queued This issues is on the AWS team's backlog label Jun 23, 2023
@RanVaknin
Copy link
Contributor

@rafak

Thanks for the clarification. I see what you are saying.

Added this to our queue.

Thanks,
Ran~

@kuhe kuhe added p2 This is a standard priority issue and removed p3 This is a minor priority issue labels Jun 23, 2023
@kuhe
Copy link
Contributor

kuhe commented Jun 24, 2023

should be fixed in releases including the not yet created https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.360.0, the next expected release is on Monday.

@github-actions
Copy link

github-actions bot commented Jul 9, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. p2 This is a standard priority issue queued This issues is on the AWS team's backlog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants