Skip to content

Commit

Permalink
Add generic try-catch block in determineID() method
Browse files Browse the repository at this point in the history
  • Loading branch information
BetTD committed Jan 23, 2023
1 parent c9bdd71 commit 18b5821
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,23 @@ private String determineID() throws IOException {
return null;
}

final String hostname = Files.readAllLines(path).get(0);
final String serverID = hostname.substring(0, 8);
try {
final String hostname = Files.readAllLines(path).get(0);
final String serverID = hostname.substring(0, 8);

final Pattern pattern = Pattern.compile("([0-9a-f]{8})");
final Matcher validator = pattern.matcher(serverID);
final Pattern pattern = Pattern.compile("([0-9a-f]{8})");
final Matcher validator = pattern.matcher(serverID);

if (!validator.matches()) {
plugin.log(Level.SEVERE, "Hostname does not look like a valid server ID. Got '" + hostname + "'.");
if (!validator.matches()) {
plugin.log(Level.SEVERE, "Hostname does not look like a valid server ID. Got '" + hostname + "'.");
return null;
}

return serverID;
} catch (Exception exception) {
plugin.log(Level.SEVERE, "Unable to get a valid hostname from the /etc/hostname file.");
exception.printStackTrace();
return null;
}

return serverID;
}
}

0 comments on commit 18b5821

Please sign in to comment.