4

My workflow sends mails when it fails using a try-catch. i have also enable concurrency and with this, when multiple jobs of the same workflow enters into a throttling stage, the new ones cancels the older ones. This throw an exception of "org.jenkinsci.plugins.workflow.steps.FlowInterruptedException" And canceled jobs also triggers the mail notification.

Now i have modified my workflow to catch the specific FlowInterruptedException exception and suppress the mail notice and let anything else to trigger the mail, like so.

node {
try {
// some stages for the workflow
}

catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e){

        echo "the job was cancelled or aborted"
         }

 catch (err){ 
         stage 'Send Notification' 
         mail (to: 'adminj...@somename.com', 
         subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.", 
              body: "Some text", 
            mimeType:'text/html'); 
         currentBuild.result = 'FAILURE' 
     } 

}

This is catching only the FlowInterruptedException and when the job really fails due to any other reason (command typo, etc), i was expecting it will be caught by the other catch and will trigger the code inside it to send the mail. But it isn't.

I think my code have some flaw in the try catch. Any idea?

UPDATE:

Just incase, if i use the below code it just send mail for just about any failures

node {
try {
// some stages for the workflow
}

catch (err){ 
         stage 'Send Notification' 
         mail (to: 'adminj...@somename.com', 
         subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.", 
              body: "Some text", 
            mimeType:'text/html'); 
         currentBuild.result = 'FAILURE' 
     } 

}
Cœur
  • 34,719
  • 24
  • 185
  • 251
OK999
  • 1,303
  • 2
  • 19
  • 35
  • I have figured out a bit here. But i would not say it as a solution. The snippet with the 2 catch 's works as long as its returning some hudson specific exceptions (may not the right term, i am not a java or groovy person). But if there is exceptions like NoSuchMethod exceptions, the catch fails – OK999 Feb 04 '16 at 01:06
  • Catching only FlowInterruptedException appears to be ambiguous, see https://issues.jenkins-ci.org/browse/JENKINS-34376?#comment-268093. – kadrach Jan 09 '17 at 23:58

2 Answers2

2

You can catch FlowInterruptedException - as you are doing now - and then check one of its causes (FlowInterruptedException#getCauses()) is org.jenkinsci.plugins.workflow.support.steps.StageStepExecution.CanceledCause, which means that the flow was interrupted while waiting to enter a stage step.

Any other combination is a legitimate error eligible to send the notification email.

amuniz
  • 3,124
  • 2
  • 19
  • 20
0

Maybe this could help. In the else statement you can put your further conditions.

try{
} catch (Exception e) {
            if (e.toString() == "org.jenkinsci.plugins.workflow.steps.FlowInterruptedException"){
                println e.toString()
                echo "job was cancelled or aborted"
            } else {
                echo "DEBUG: caught error."
                println e.toString()
                currentBuild.result = 'FAILURE'
            }
        }