Tengo un caso interesante en el que Promise.reject() no está atrapado en .catch() de la persona que llama, sino en try{ }catch{ } .
callDB() llama a una función pg db para realizar una Insert , cuando se produce un error en tiempo de ejecución (violación de clave única), va a .then().catch() :
const callDB = (param) => { return new Promise((resolve, reject) => { var sql = "...myInsert($1, $2)"; var dataValue = [param.p1, paramp2]; db.any(sql, dataValue) .then ( good => resolve(good), bad => reject(bad) ) // Run-Time Err like unique-key violation caught here, returns a reject. .catch ( dbEr => { return reject(dbEr); }) }); } f2() , la persona que llama, su callDB().catch() no detecta el reject , pero el try{} catch{tce} :
async function f2() { try { ... // callDB's reject NOT caught here. const good2 = await callDB(param).catch((e2)=>{ return Promise.reject(e2); }); ... } catch(tce) { // callDB's reject caught here! return Promise.reject(tce); } } Una función de Azure llama a f2() :
module.exports = async function f1(context) { try { await f2().then ( good => ... bad => { // Caught here as Expected! ... context.res = {status: 401, body: bad}; } ) .catch ( (e1) => { context.res = {status: 401, body: "never reach e1"}; } ) } catch (etry) { context.res = {status: 401, body: "never reach etry"}; } }Mis preguntas son:
reject de callDB entra en try{} catch{} de f2 ?.then().catch() y try{} catch{} ?