Home > Enterprise >  Is it possible to suppress the stack trace from a thrown Exception in groovy declarative pipelines?
Is it possible to suppress the stack trace from a thrown Exception in groovy declarative pipelines?

Time:10-13

I have a declarative pipeline with an input step where it is possible to put the wrong input. I have wrapped the stage in a retry block and thrown Exceptions if the input is found to be invalid.

if (isValid == 1) {
   println("Erroring out due to invalid change ticket")
   throw new Exception("Please rerun the job and enter a valid change ticket")
} else {
   println("CHG ticket is valid")

If this exception is thrown the stage is restarted.

The pipeline console spits this out:

ERROR: Execution failed
java.lang.Exception: Please rerun the job and enter a valid change ticket
    at WorkflowScript.run(WorkflowScript:197)
    at ___cps.transform___(Native Method)
    at sun.reflect.GeneratedConstructorAccessor1164.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:208)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onNewInstance(GroovyInterceptor.java:42)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:172)
    at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:205)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:210)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:21)
Retrying
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 1 day 0 hr
[Pipeline] {
[Pipeline] input
Input requested

But I only care for the message I passed with the Exception.

Is there a way to suppress the stacktrace from this Exception in the Jenkins console?

CodePudding user response:

Use error step (Error Step)

error: Error signal Signals an error. Useful if you want to conditionally abort some part of your program. You can also just throw new Exception(), but this step will avoid printing a stack trace.

if (isValid == 1) {
  println("Erroring out due to invalid change ticket")
  error("Please rerun the job and enter a valid change ticket")
} else {
  println("CHG ticket is valid")
}
  • Related