2

how do I get the actual exception of a failed pipeline step/plugin? I have the following pipeline code

try {
    checkout(
        scm: [ ... ]
    )
} catch (error) {
    echo error.getClass()
    echo error.getMessage()
}

which always results in

class hudson.AbortException
Error fetching remote repo 'origin'

But running without try/catch it would result in:

hudson.plugins.git.GitException: Failed to fetch from https://git.repo.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1003)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1245)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1309)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress --prune --depth=1 -- https://git.repo.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: fatal: Unable to create '/var/jenkins_home/workspace/some_space/.git/shallow.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2734)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2111)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:623)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:158)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler$GitCommandMasterToSlaveCallable.call(RemoteGitImpl.java:151)
    at hudson.remoting.UserRequest.perform(UserRequest.java:211)
    at hudson.remoting.UserRequest.perform(UserRequest.java:54)
    at hudson.remoting.Request$2.run(Request.java:377)
    at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:78)
    ... 4 more
    Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to slave-lpdkwpci13vm
        at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1784)
        at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
        at hudson.remoting.Channel.call(Channel.java:1000)
        at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.execute(RemoteGitImpl.java:143)
        at jdk.internal.reflect.GeneratedMethodAccessor1484.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.jenkinsci.plugins.gitclient.RemoteGitImpl$CommandInvocationHandler.invoke(RemoteGitImpl.java:129)
        at com.sun.proxy.$Proxy209.execute(Unknown Source)
        at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1001)
        at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1245)
        at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1309)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
        at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        ... 4 more

so the real exception is:

hudson.plugins.git.GitException: Command "git fetch --tags --force --progress --prune --depth=1 -- https://git.repo.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: fatal: Unable to create '/var/jenkins_home/workspace/some_space/.git/shallow.lock': File exists.

Is there a possibility to catch a GitException instead of hudson.AbortException or get the message from stderr.

I already tried things like error.getCause()

2 Answers 2

1

What you are referring to here is exception chaining. I don't think you can catch a specific exception in the chain. You can walk the chain to see all the exceptions. To get the last exception, the GitException, you can use this code.

2
  • 1
    Looks promising. Will try it out. Thank you!
    – Haagy
    Commented May 8, 2023 at 10:21
  • Did you manage to access the GitException? I get null on the first e.getCasue() e is the abort exception Commented Jun 27, 2023 at 9:16
0

Pipelines use regular Groovy, so you would use the normal Groovy approach for this:

try { 
   checkout() 
} catch(hudson.plugins.git.GitException e) { 
   // handle a GitException 
} catch(hudson.AbortException e) { 
   // handle an AbortException 
} finally {
   // cleanup
}
2
  • thansk for pointing that out. But as I tried to point out with echo error.getClass() the class is always hudson.AbortException even tho a hudson.plugins.git.GitException was thrown as shown in the logs
    – Haagy
    Commented May 5, 2023 at 7:01
  • @Haagy I was a bit confused by your question so I tried to answer the part in bold ("Is there a possibility to catch a GitException instead of hudson.AbortException..."). But it looks like the other answer better addressed your actual issue so hopefully you were able to get this figured out :)
    – jayhendren
    Commented May 11, 2023 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.