1

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

jun
  • 192
  • 15
  • instead of `throw err;` try `return Promise.reject(err);` – r3wt Nov 19 '20 at 17:27
  • also, you need to `await platform.process()` or your outer try catch does nothing. also for this to work, you would need to return the promise from `platform.process()` – r3wt Nov 19 '20 at 17:28

2 Answers2

1

your problem is you are not returning the promise from platform.process(), nor are you awaiting the response from that async function. thus your catch block code will never run because the code that throws from platform.process() is async.

here is the way i would handle that:

try {
  await platform.process(someParam);
} catch (err) {
  console.log('caught error');
  // do further handling here
}

platform.process (below is an example since we can't see the way platform.process is coded):

function process(someParam) {
  return SomeLibrary.request( {content} )
  .then( (response) => console.log(response) )
  .catch( (err) => { 
    console.log('throwing'); 
    return Promise.reject(err);
  } );
}

OR (an alternative way to write platform.process):

async function process(someParam) {
  try{
    const response = await SomeLibrary.request( {content} );
    console.log(response);
  catch(err) {
    console.log(err);
    return Promise.reject(err);
  }
}
r3wt
  • 4,642
  • 2
  • 33
  • 55
  • wow, you are right. it's precisely because process() isn't the one returning the promise. I have made use of the Promise.reject(err) method and it worked! Much thanks, I have been stuck with this for the entire day! – jun Nov 19 '20 at 17:47
0

Either remove the try catch block or throw the error in the catch block. Possibly missing await for promise of process function

try {
  await platform.process(someParam);
} catch (err) {
  console.log('caught error');
  // throw error regardless
  throw err;
}
Julian Kleine
  • 1,539
  • 6
  • 14
  • i'm not sure how that will work; currently the 'caught error' message isn't even printed, which implies that the error wasn't even thrown successfully from the catch block in the promise in the first place, right? – jun Nov 19 '20 at 17:13
  • SomeLibrary.request( {content} ) is that try catch block above in this function? – Julian Kleine Nov 19 '20 at 17:16
  • probably it's the missing await in the try block? – Julian Kleine Nov 19 '20 at 17:16
  • yes, the request() method is called inside process(), which is in the try catch block. – jun Nov 19 '20 at 17:19
  • i tried adding the 'await' keyword before platform.process(), still the same; the console log isn't printed so the error still isn't thrown. – jun Nov 19 '20 at 17:20
  • Okay, can you please reproduce your error in a sandbox? https://codesandbox.io/ – Julian Kleine Nov 19 '20 at 17:25
  • Or add the full implementation of `platform.process(someParam)` – Julian Kleine Nov 19 '20 at 17:26
  • hi Julian, another user has managed to provide the solution. thanks for responding so promptly, i still appreciate the help nonetheless – jun Nov 19 '20 at 17:48