问题描述
我在 sample.ts (打字稿)中有以下代码,其中exe始终返回退出代码0(成功),1(失败)或2(未知错误)。我希望 validateFunc 调用能够正确返回退出代码并降落在下一行,但是它会转到catch块以获取非零退出代码。知道导致这种行为的原因是什么吗?
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);
}
解决方法
检查 $ ErrorActionPreference 的值。如果不是Stop,则捕获将无法运行。
尝试在尝试块之前添加$template->template_json = json_decode($template->template_json);
,它应该可以正确捕获错误。
有关更多详细信息,请查看此错误:Powershell try block not working on VSTS build