Skip to content

Commit

Permalink
fix(Message sent): Improve error handling
Browse files Browse the repository at this point in the history
Fixes JENKINS-57106 by wrapping getRocketClient in a try-catch block. If failOnError is true, the exception is propagated and the build will fail.
  • Loading branch information
hypery2k committed Apr 20, 2019
1 parent ba1c4ee commit ec36a79
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,33 @@ public void uncaughtException(Thread t, Throwable e) {
// placing in console log to simplify testing of retrieving values from global config or from step field; also used for tests
listener.getLogger().println(Messages.RocketSendStepConfig(channel, step.message));

RocketClient rocketClient = getRocketClient(server, trustSSL, user, password, channel, webhookToken, webhookTokenCredentialId);
// getRocketClient needs to be wrapped inside a try-catch because it can fail too if the target RocketChat server does not behave properly.
try {
RocketClient rocketClient = getRocketClient(server, trustSSL, user, password, channel, webhookToken, webhookTokenCredentialId);

String msg = step.message;
if (!step.rawMessage) {
msg += "," + run.getFullDisplayName() + "," + jenkinsUrl + run.getUrl() + "";
}
String msg = step.message;
if (!step.rawMessage) {
msg += "," + run.getFullDisplayName() + "," + jenkinsUrl + run.getUrl() + "";
}

boolean publishSuccess = rocketClient.publish(msg, step.emoji, step.avatar,
MessageAttachment.convertMessageAttachmentsToMaps(step.attachments));
if (!publishSuccess && step.failOnError) {
throw new AbortException(Messages.NotificationFailed());
}
else if (!publishSuccess) {
listener.error(Messages.NotificationFailed());
boolean publishSuccess = rocketClient.publish(msg, step.emoji, step.avatar,
MessageAttachment.convertMessageAttachmentsToMaps(step.attachments));
if (!publishSuccess && step.failOnError) {
throw new AbortException(Messages.NotificationFailed());
}
else if (!publishSuccess) {
listener.error(Messages.NotificationFailed());
}
return null;

} catch (RocketClientException rce) {
if (step.failOnError) {
throw rce;
} else {
listener.error(Messages.NotificationFailedWithException(rce));
return null;
}
}
return null;
}

//streamline unit testing
Expand Down

0 comments on commit ec36a79

Please sign in to comment.