1

I have below code in sample.ts (typescript) where the exe always returns an exit code of 0 (success), 1 (failure) or 2 (unknown errors). I would expect the validateFunc call to return an exit code properly and land in the next line but instead it goes to the catch block for non-zero exit codes. Any idea what causes that behavior?

try

{

...{some code}

 let exitCode = await validateFunc("sample1", "sample2");

if(exitcode!=0)
{
  // expecting toland here when tool returns non-zero exit code
}

..{some code here}

}
catch (error)

{

 // Above call to validateFunc lands here for non-zero exit codes

}

export async function validateFunc(param1: string, param2: string) {

   
      {some code here}

    return await tl.exec(`${validationToolRoot}\\<Exename>`, validationArgs);
}
MAK
  • 307
  • 2
  • 12

1 Answers1

0

Check the value of $ErrorActionPreference. If it's not Stop, the catch won't run.

Try add $ErrorActionPreference = "Stop" before the try-block and it should catch the error properly.

For more details, kindly take a look at this error: Powershell try block not working on VSTS build

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks. This is not a powershell script but typescript. And my question primarily here is why is it going into the catch block when it's not supposed to according to my expectations? – MAK Nov 04 '20 at 17:26
  • @MAK Thanks for your kindly update. What did you get in the log? Could you share related part log with `system.debug=ture` in Azure DevOps pipeline side? – PatrickLu-MSFT Nov 06 '20 at 10:01