I created a custom exception class and put it inside src/org/team/pipeline/TestResultFailed.groovy
:
package org.team.pipeline
public class TestResultFailed extends Exception {
// Parameterless Constructor
public TestResultFailed() {}
// Constructor that accepts a message
public TestResultFailed(String message) {
super(message);
}
}
The only issue is the class (and thus even exception) cannot be easily imported from shared library to Jenkins pipeline. Therefore, it's not possible to directly throw exception from Jenkins pipeline.
To bypass that, I tried to "wrap" my new exception into shared library function in vars/teamUtils.groovy
:
import org.team.pipeline.*
def getTestResultFailedClass() {
return TestResultFailed
}
And then I import and use it in Jenkinsfile as follows:
TestResultFailed = teamUtils.getTestResultFailedClass()
try {
throw new TestResultFailed.TestResultFailed("error")
} catch (TestResultFailed err) {
log.info("Exception caught: ${err}")
}
However, I get this error:
unable to resolve class TestResultFailed.TestResultFailed
and no matter what I tried I'm still getting this error. Any help is very appreciated.