javascript - NodeJS: Unhandled promise rejection -
i'm having little problem , after debugged app noticed file that's causing problem, returning me unhandledpromiserejection
'use strict' const connection = require('../models/'), oracledb = require('oracledb'), conexion = oracledb.getconnection(connection) oracledb.outformat = oracledb.object; module.exports = { index(req, res) { conexion.then(con => { return con.execute( `select id_application, name, description, creation_date ` + `from application ` ).then(bucket => { return con.execute( `select id_definition, id_application, field_name_original, field_name_new, column_name, position, id_type_data, field_size, creation_date, description, filter, visible ` + `from definition ` ).then(definitions => { res.status(200).json(creajson(bucket, definitions)) }).catch(error => { return res.status(500).json({'message': error}) }) }).catch(err => { return res.status(500).json({'message': err}) }) }).catch(err => { return res.status(500).json({'message': err}) }) }, create(req, res) { }, update(req, res) { } } const dorelease = (connection) => { connection.close((err) => { if(err) console.error(err.message); }) } const creajson = (buckets, definitions) => { var df = new array() buckets['rows'].map(obj => { definitions['rows'].map(def => { if(obj['id_application'] == def['id_application']) df.push(def) }) obj['definitions'] = df df = [] }) return buckets.rows } after unhandledpromiserejection being followed by: error: ora-12170: tns:connect timeout occurred
(node:1270) deprecationwarning: unhandled promise rejections deprecated. in future, promise rejections not handled terminate node.js process non-zero exit code. i looked solutions, says promises not catching correctly don't see problem them. other suggestion?
any welcome.
thanks
const connection = require('../models/'), oracledb = require('oracledb'), conexion = oracledb.getconnection(connection) is setting conexion promise returned call .getconnection made when entire source file executed (in response being required).
conexion has no handlers @ point. handlers added later when indexmethod of exported {index, create, update} object called.
hence connection timeout in between source file being required , index being called produce unhandled rejection error.
obviously adding catch clause such as
conexion = oracledb.getconnection(connection).catch( onrejected) should fix error, how recovery want put coding onrejected you.
edit:
a less obvious approach satisfying v8's version of how handle uncaught promise rejection provide dummy handler thwart it:
conexion = oracledb.getconnection(connection); conexion.catch(()=>undefined); // nothing catch handler. here second line adds handler conexion promise, making "handled", prevents ever becoming uncaught promise rejection. promise returned catch superfluous , not recorded, fulfilled if no-operation catch handler ever called.
now promise held in conexion can rejected before index called without generating exception. whether best way code promise topology particular application different question - may wish address connection timeout earlier.
Comments
Post a Comment