For context, I'm working with NextJs.
Basically from one of my api pages /api/sendData.ts, I am doing a method call process() which belongs to a separate class platform.ts.
Structure of API code (in sendData.ts):
try {
platform.process(someParam);
} catch (err) {
console.log('caught error');
// do further handling here
}
Implementation of process() in platform.ts:
SomeLibrary.request( {content} )
.then( (response) => console.log(response) )
.catch( (err) => { console.log('throwing'); throw err; } );
Currently, this does not throw the error back (or at least I'm not able to catch from the caller), but instead prints out "throwing", followed by "UnhandledPromiseRejectionWarning: StatusCodeError: 401 .....". I have also tried this:
.catch( function(err) { throw err; } );
From what I've read, seems like throwing an error in the catch block is actually "returning another promise", so the promise chain isn't really resolved(?) or complete.
How can I throw the err in the catch block so that this error can be propagated back to sendData.ts for handling? Is it even possible to bubble the error back up so I can handle the error from my server-side (./api in nextjs)
P.S. This thread highlights the exact same error I'm facing and none of the responses were able to resolve my issue: UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block