programming について調べたことを整理していきます

Node.js: UnhandledPromiseRejectionWarning

In node.js script, without attaching any error handlers to the Promise within a event loop, "unhandledRejection" is emitted.

The error log is as follows.

(node:57974) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:57974) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:57974) UnhandledPromiseRejectionWarning: Error: Request failed with status code 429
    at createError (/Users/test/Documents/my-repo/test-module/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/Users/test/Documents/my-repo/test-module/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/Users/test/Documents/my-repo/test-module/node_modules/axios/lib/adapters/http.js:201:11)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

In this case, we can't know the actual reason of this error.

To print the actual reason, we just only set handler for the "unhandledRejection".

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at:', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here

Process | Node.js v14.5.0 Documentation