如何尝试捕获实际错误而不是hudson.AbortException

时间:2019-05-16 11:49:23

标签: jenkins jenkins-groovy

我在我的jenkins文件中使用try catch,以检查是否抛出了特定错误以及是否具有某些功能

try {
      // code that throws an error

    } catch (ex) {
      echo 'an error occurred'
      echo "ex: ${ex}"
      if (ex == Exception1) {
         // do stuff
      }

      if (ex == Exception2) {
        // do other stuff
      }
    }

我的问题是所捕获的错误是hudson.AbortException,消息是“脚本返回了退出代码1”。

如何捕获抛出的实际错误而不是哈德森/詹金斯包装器?

2 个答案:

答案 0 :(得分:0)

“脚本返回退出代码1”实际上是错误消息。 您应该检查正在运行的脚本,然后对其进行修改以在退出前打印内部脚本错误

答案 1 :(得分:0)

如果您使用语法sh脚本运行shell脚本:'echo hello'Ref

然后,如参考文档中所述,应使用set + e和set -e运行它。在此,即使您之间遇到错误,set + e和set -e也将使程序保持运行,这应该在shell本身中进行处理。如果在shell中使用2>&1,则返回的值也将包含错误。

如果此返回值包含Exception,则可以在try本身中引发单独的错误。

例如:

try{
    def returnedVal = sh script: 'some exception raising code'
    if (returnedVal.toLowerCase().contains('exception1')){
        error("got Exception1")
    }
} catch(Excpetion ex){
    //handle Exception
}