Skip to content

Commit

Permalink
fix(error_handling): Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hypery2k committed Dec 9, 2016
1 parent 04038fc commit 4c51838
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package jenkins.plugins.rocketchatnotifier;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -19,6 +20,7 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import sun.security.validator.ValidatorException;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -328,16 +330,18 @@ public FormValidation doTestConnection(@QueryParameter("rocketServer") final Str
}
RocketChatClient rocketChatClient = getRocketChatClient(targetServerUrl, targetUsername, targetPassword);
String message = "RocketChat/Jenkins plugin: you're all set on " + targetBuildServerUrl;
boolean success = false;
boolean success;
try {
rocketChatClient.send(targetChannel, message);
success = true;
} catch (IOException e) {
success = false;
}
return success ? FormValidation.ok("Success") : FormValidation.error("Failure");
} catch (ValidatorException e) {
return FormValidation.error(e, "SSL error");
} catch (Exception e) {
return FormValidation.error("Client error : " + e.getMessage());
return FormValidation.error(e, "Client error : " + e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
import jenkins.plugins.rocketchatnotifier.model.Room;
import jenkins.plugins.rocketchatnotifier.model.Rooms;
import org.json.JSONObject;
import sun.security.validator.ValidatorException;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
* Created by mreinhardt on 08.09.16.
*/
public class RocketClientImpl implements RocketClient {

private static final Logger LOG = Logger.getLogger(RocketClientImpl.class.getName());

public static final String API_PATH = "/api/";

private RocketChatClient client;
Expand All @@ -37,8 +41,11 @@ public RocketClientImpl(String serverUrl, String user, String password, String c

public boolean publish(String message) {
try {
LOG.fine("Starting sending message to channel " + this.channel);
this.client.send(this.channel, message);
return true;
} catch (ValidatorException e) {
return false;
} catch (IOException e) {
return false;
}
Expand Down Expand Up @@ -94,11 +101,11 @@ private <T> T authenticatedGet(String method, Class<T> reponseClass) throws IOEx
}
}

private void authenticatedPost(String method, Object request) throws IOException {
private void authenticatedPost(String method, Object request) throws ValidatorException, IOException {
this.authenticatedPost(method, request, (Class) null);
}

private <T> T authenticatedPost(String method, Object request, Class<T> reponseClass) throws IOException {
private <T> T authenticatedPost(String method, Object request, Class<T> reponseClass) throws ValidatorException, IOException {
try {
HttpResponse e = Unirest.post(this.serverUrl + method).header("X-Auth-Token", this.xAuthToken).header("X-User-Id", this.xUserId).header("Content-Type", "application/json").body(this.jacksonObjectMapper.writeValueAsString(request)).asString();
if (e.getStatus() == 401) {
Expand Down Expand Up @@ -151,7 +158,7 @@ private JSONObject getVersions() throws IOException {
return this.lazyVersions;
}

public void send(String roomName, String message) throws IOException {
public void send(String roomName, String message) throws ValidatorException, IOException {
Room room = this.getRoom(roomName);
if (room == null) {
throw new IOException(String.format("unknown room : %s", new Object[]{roomName}));
Expand All @@ -160,7 +167,7 @@ public void send(String roomName, String message) throws IOException {
}
}

public void send(Room room, String message) throws IOException {
public void send(Room room, String message) throws ValidatorException, IOException {
this.authenticatedPost("rooms/" + room.get_id() + "/send", new Message(message));
}

Expand Down

0 comments on commit 4c51838

Please sign in to comment.