Catch errors in promise resolved with stream concatenation in node js

609 views Asked by At

I need to catch errors thrown inside a stream event in promise resolution. Something like

function foo(){
 return new Promise((resolve,reject) => {
  resolve(res.pipe(transfomrStream).pipe(through2.obj(function(obj, enc, callback) {
    on('end', ()=>{ 
        await httpReq(...)
          .then((crudRes) => assert.strictEqual(somerthing))
      })
     )
   })
 })
}

how could i catch assert failure? I've tried to return error inside the last pipe() or in function calling but i only get unhandled promise rejection

1

There are 1 answers

0
crellee On

If you surround your code with a try catch you can at least see what your error is:

function foo(){
try{
 return new Promise((resolve,reject) => {
  resolve(res.pipe(transfomrStream).pipe(through2.obj(function(obj, enc, callback) {
    on('end', ()=>{ 
        await httpReq(...)
          .then((crudRes) => assert.strictEqual(somerthing))
      })
     )
   })
 })
 }
 catch(e) {
  console.log(e)
  reject()
}
}

Also, if you have an await you cannot use .then(). And you will have to make your function async if you're doing await.