我想使用readYaml方法来验证Yaml文件。如果该文件无效,则引发以下异常。但是,我想抓住这一点,并在必要时用单独的错误消息替换它。不幸的是,我不知道在这里必须捕捉哪个异常
while parsing a block mapping
in 'reader', line 3, column 1:
logging:
^
expected <block end>, but found BlockMappingStart
in 'reader', line 5, column 2:
server:
^
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:573)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:159)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:149)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:225)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:156)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:123)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.composer.Composer.getNode(Composer.java:85)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:106)
at org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.Yaml$1.next(Yaml.java:472)
at org.jenkinsci.plugins.pipeline.utility.steps.conf.ReadYamlStep$Execution.doRun(ReadYamlStep.java:133)
at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
at hudson.security.ACL.impersonate(ACL.java:290)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
我的代码:
fileContent = sh returnStdout: true, script: "sudo ssh devadmin@${hostname} cat ${file}"
println("*************************** Pruefen der Datei ${file} auf dem Host ${hostname} ***********************************")
if (file.contains("yml")) {
try {
def datas = readYaml text: fileContent
message = "${file} ist valide"
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
println("\033[32m ${message} \033[0m")
}
}catch (java.io.NotSerializableException e) {
message = "Fehler beim Validieren von ${file} :"+ e
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
println("\033[31m ${message} \033[0m")
}
}
}