vitaly-t/pg-promise-demo

Any error causes an UnhandledPromiseRejectionWarning and postman is stuck

slidenerd opened this issue · 2 comments

  • Fantastic work with your library and answers on stackoverflow under [pg-promise]
  • I just wanted to point out that your repo structure is solid when things are smooth
  • There are some problems when there is an error though
  • Say a query fails for some reason, the UserRepository is not handling any errors
  • It gets straight to the API endpoint handling router in your index.js
  • Here also you have not handled any errors

For example I tested a non existing user to check how the code reacts

(node:6745) UnhandledPromiseRejectionWarning: error: role "super_duper_root" does not exist
    at Parser.parseErrorMessage (/Users/zup/Desktop/code/ACTIVE/super_duper/node_modules/pg-protocol/dist/parser.js:241:15)
    at Parser.handlePacket (/Users/zup/Desktop/code/ACTIVE/super_duper/node_modules/pg-protocol/dist/parser.js:89:29)
    at Parser.parse (/Users/zup/Desktop/code/ACTIVE/super_duper/node_modules/pg-protocol/dist/parser.js:41:38)
    at Socket.<anonymous> (/Users/zup/Desktop/code/ACTIVE/super_duper/node_modules/pg-protocol/dist/index.js:8:42)
    at Socket.emit (events.js:311:20)
    at Socket.EventEmitter.emit (domain.js:482:12)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:6745) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:6745) [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.
  • If you use POSTMAN to query the endpoint, the request gets stuck because we did not return any response in case of an error

  • Do you think the below one would be a better structure?


router.get('/', async (req, res) => {
  try {
    res.json(await db.users.all())
  } catch (error) {
    logger.error(error)
    return res.json(false)
  }
})

See disclaimer at the top of the API file:

/////////////////////////////////////////////////////////////////////////////
// IMPORTANT:
//
// Do not re-use the HTTP-service part of the code from here!
// It is an over-simplified HTTP service with just GET handlers, because:
//
// 1. This demo is to be tested by typing URL-s manually in the browser;
// 2. The focus here is on a proper database layer only, not an HTTP service.
/////////////////////////////////////////////////////////////////////////////

Apologies my bad, thank you