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: '[email protected]',
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: '[email protected]',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.",
body: "Some text",
mimeType:'text/html');
currentBuild.result = 'FAILURE'
}
}
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.