Answers: <p>What you are referring to here is <a href="https://www.baeldung.com/java-chained-exceptions" rel="nofollow noreferrer">exception chaining</a>. 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 <code>GitException</code>, you can use this <a href="https://stackoverflow.com/a/28565320/3888850">code</a>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-05-08T10:21:15.473Z
Answers: Looks promising. Will try it out. Thank you!
Answers: Haagy
Answers: CC BY-SA 4.0
Answers: 2023-06-27T09:16:21.450Z
Answers: Did you manage to access the GitException?
  I get null on the first `e.getCasue()` `e` is the abort exception
Answers: Barel elbaz
Answers: CC BY-SA 4.0
Answers: 2023-05-06T10:25:50.253Z
Answers: 2023-05-06T10:25:50.253Z
Answers: Levi
Answers: <p>Pipelines use regular Groovy, so you would use the normal Groovy approach for this:</p>
  <pre><code>try { 
     checkout() 
  } catch(hudson.plugins.git.GitException e) { 
     // handle a GitException 
  } catch(hudson.AbortException e) { 
     // handle an AbortException 
  } finally {
     // cleanup
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-05-05T07:01:57.503Z
Answers: 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
Answers: Haagy
Answers: CC BY-SA 4.0
Answers: 2023-05-11T22:42:49.507Z
Answers: @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 :)
Answers: jayhendren
Answers: CC BY-SA 4.0
Answers: 2023-05-03T20:48:21.667Z
Answers: 2023-05-03T20:57:27.690Z
Answers: 2023-05-03T20:57:27.690Z
Answers: jayhendren
Body: <p>how do I get the actual exception of a failed pipeline step/plugin?
  I have the following pipeline code</p>
  <pre><code>try {
      checkout(
          scm: [ ... ]
      )
  } catch (error) {
      echo error.getClass()
      echo error.getMessage()
  }
  </code></pre>
  <p>which always results in</p>
  <pre><code>class hudson.AbortException
  Error fetching remote repo 'origin'
  </code></pre>
  <p>But running without try/catch it would result in:</p>
  <pre><code>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 &quot;git fetch --tags --force --progress --prune --depth=1 -- https://git.repo.git +refs/heads/*:refs/remotes/origin/*&quot; 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
  </code></pre>
  <p>so the real exception is:</p>
  <pre><code>hudson.plugins.git.GitException: Command &quot;git fetch --tags --force --progress --prune --depth=1 -- https://git.repo.git +refs/heads/*:refs/remotes/origin/*&quot; returned status code 128:
  stdout: 
  stderr: fatal: Unable to create '/var/jenkins_home/workspace/some_space/.git/shallow.lock': File exists.
  </code></pre>
  <p><strong>Is there a possibility to catch a <code>GitException</code> instead of <code>hudson.AbortException</code> or get the message from <code>stderr</code>.</strong></p>
  <p>I already tried things like <code>error.getCause()</code></p>
  
Tags: <jenkins><jenkins-pipeline><jenkins-plugins>
Title: Jenkins try-catch the actual error instead of hudson.AbortException
UserDisplayName: Haagy
ThreadText: # Jenkins try-catch the actual error instead of hudson.AbortException
  
  (Asked by: username_0 on 2023-05-03)
  
  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()`
  
  **Tags:** jenkins, jenkins-pipeline, jenkins-plugins
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-05-06. Score: 1 | Accepted answer
  
  What you are referring to here is [exception chaining](https://www.baeldung.com/java-chained-exceptions). 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](https://stackoverflow.com/a/28565320/3888850).
  
  > **Comments:**
  > **username_0**: Looks promising. Will try it out. Thank you!  
  > **username_2**: Did you manage to access the GitException? I get null on the first `e.getCasue()` `e` is the abort exception  
  
  ---
  
  ### Answer by username_3 on 2023-05-03. Score: 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
  }
  
  ```
  
  > **Comments:**
  > **username_0**: 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  
  > **username_3**: @username_0 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 :)  
  
  ---
  
  