Home > Software engineering >  Throwing error from if statement inside try not working
Throwing error from if statement inside try not working

Time:08-25

I have the below function.

private terminateUser = async (req): Promise<any> => {
  try{
    if(req.oper=='cancel'){
      if(!('termStat' in corporate.data)){
       console.log('here');
       throw new ServerError({errorcode:121})
      } else {
        reqbody= this.generateReqbody(req);
       }
    }
   return await Promise.resolve(this.config.message);
  } catch(err) {
    throw new ServerError(errorcode:100);
    }

Here , I am getting the console message 'here' inside the if statement !('termStat' in corporate.data)

But it is not throwing the errorcode 121 instead it is throwing the errorcode 100 inside catch. How can I throw the exact errorcode from if statement

CodePudding user response:

the alternative is

try{
    //  ....
    if(!('termStat' in corporate.data)){
        console.log('here');
        throw 'error1';
    }
    // ....
} catch(err){
   if(err==='error1')throw new ServerError({errorcode:121});
   throw new ServeError(errorcode:100);
}

CodePudding user response:

**strong text**  //you can try
private terminateUser = async (req): Promise<any> => {
  try{
    if(req.oper=='cancel'){
      if(!('termStat' in corporate.data)){
       console.log('here');
       throw new ServerError({errocode:121})
      } else {
        reqbody= this.generateReqbody(req);
       }
    }
   return await Promise.resolve(this.config.message);
  } catch(err) {
    throw err
    }
  • Related