Search code examples
node.jses6-promise

Pass/Return a value in reject() when an error occurs


How can I return a value using reject() when using promises?

I am trying to access an AWS S3 bucket from my node.js code. Here's my code

If the operation is successful, I resolve the promise with the s3Info object. If there is an error, I want to reject the promise, again returning the s3Info object, but with the error message. I am unable to get the s3Info{} with the error message when an error occurs. How can I achieve this?

I call it from another module as follows-

log.s3 = await pushToS3(bucketName, filename, data)

...where log is an object and s3 is its property (also an object)

When I pass the correct parameters, the data gets pushed to s3 and the promise gets resolved and I get the following in log.s3-

"s3": { "success": true, "message": { "ETag": "someETag", "VersionId": "someVersionId" } }

but when I enter wrong parameters, say a bucketName that doesn't exist, I get an error, which I add to the s3Info object in the pushToS3 function. I just can't get that object back to the calling function.

This is what I expect when an error occurs-

{ success: false, message: { AccessDenied: Access Denied at Request.extractError (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\services\s3.js:585:35) at Request.callListeners (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\state_machine.js:14:12) at F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\request.js:38:9) at Request. (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (F:\workspace\webScraping\botsWorkingOnKeywords\node_modules\aws-sdk\lib\sequential_executor.js:116:18) message: 'Access Denied', code: 'AccessDenied', region: null, time: 2019-04-16T04:07:30.545Z, requestId: 12345, extendedRequestId: 'NhyakPOYrMZ4RoRrwaqFSXZFx2c2lrb4+x+HzN/oQiL+7+AyG9RnyXA3CLbJfdOj0GZiIUJSB3U=', cfId: undefined, statusCode: 403, retryable: false, retryDelay: 74.88976302089922 } }


Solution

  • In the code where you are calling your function, you would wrap the function call in a try/catch statement. When you call reject() in your function, it will throw an error. You can catch that error and get the data you passed to reject() from the error object, transform it and return whatever is appropriate

    async function main() {
      try {
        // ... do your things
        log.s3 = await pushToS3(bucketName, filename, data)
        return someValue;
      } catch (error) {
        console.log(error);
        return error;
      }
    }