Skip to content

Commit

Permalink
Proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DevLeoko committed Mar 27, 2020
1 parent f768a45 commit 396b2fb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/main/java/me/leoko/advancedban/Universal.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
Expand Down Expand Up @@ -337,18 +339,26 @@ public void debug(Object msg) {
debugToFile(msg);
}

public void debugException(Exception exc){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exc.printStackTrace(pw);

debug(sw.toString());
}

/**
* Debug.
*
* @param ex the ex
*/
public void debug(SQLException ex) {
public void debugSqlException(SQLException ex) {
if (mi.getBoolean(mi.getConfig(), "Debug", false)) {
debug("§7An error has occurred with the database, the error code is: '" + ex.getErrorCode() + "'");
debug("§7The state of the sql is: " + ex.getSQLState());
debug("§7Error message: " + ex.getMessage());
ex.printStackTrace();
}
debugException(ex);
}

private void debugToFile(Object msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void setup(boolean useMySQLServer) {
+ " Issue tracker: https://github.com/DevLeoko/AdvancedBan/issues\n"
+ " \n"
);
Universal.get().debugSqlException(ex);
}
}

Expand All @@ -120,7 +121,7 @@ public void shutdown() {
}
} catch (SQLException ex) {
Universal.get().log("An unexpected error has occurred turning off the database");
Universal.get().debug(ex);
Universal.get().debugSqlException(ex);
}
}

Expand All @@ -145,6 +146,7 @@ private void connectMySQLServer() {
+ " Issue tracker: https://github.com/DevLeoko/AdvancedBan/issues \n"
+ " \n"
);
Universal.get().debugSqlException(exc);
failedMySQL = true;
}
}
Expand Down Expand Up @@ -194,7 +196,7 @@ private synchronized ResultSet executeStatement(String sql, boolean result, Obje
+ "error in: https://github.com/DevLeoko/AdvancedBan/issues"
);
Universal.get().debug("Query: \n" + sql);
Universal.get().debug(ex);
Universal.get().debugSqlException(ex);
}
return null;
}
Expand All @@ -210,7 +212,7 @@ public boolean isConnectionValid(int timeout) {
return connection.isValid(timeout);
} catch (SQLException ex) {
Universal.get().log("An unexpected error has occurred with the database.");
Universal.get().debug(ex);
Universal.get().debugSqlException(ex);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public InterimData load(String name, String uuid, String ip) {

} catch (SQLException ex) {
universal.log("An error has occurred loading the punishments from the database.");
universal.debug(ex);
universal.debugSqlException(ex);
}
return new InterimData(uuid, name, ip, punishments, history);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public List<Punishment> getPunishments(String target, PunishmentType put, boolea
}
} catch (SQLException ex) {
universal.log("An error has occurred getting the punishments for " + target);
universal.debug(ex);
universal.debugSqlException(ex);
}
}
return ptList;
Expand All @@ -164,7 +164,7 @@ public List<Punishment> getPunishments(SQLQuery sqlQuery, Object... parameters)
} catch (SQLException ex) {
universal.log("An error has occurred executing a query in the database.");
universal.debug("Query: \n" + sqlQuery);
universal.debug(ex);
universal.debugSqlException(ex);
}
return ptList;
}
Expand All @@ -186,7 +186,7 @@ public Punishment getPunishment(int id) {
} catch (SQLException ex) {
universal.log("An error has occurred getting a punishment by his id.");
universal.debug("Punishment id: '" + id + "'");
universal.debug(ex);
universal.debugSqlException(ex);
}
return pt == null || pt.isExpired() ? null : pt;
}
Expand Down Expand Up @@ -302,7 +302,7 @@ public int getCalculationLevel(String uuid, String layout) {

} catch (SQLException ex) {
universal.log("An error has occurred getting the level for the layout '" + layout + "' for '" + uuid + "'");
universal.debug(ex);
universal.debugSqlException(ex);
}
return i;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/leoko/advancedban/utils/Punishment.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void create(boolean silent) {
}
}
} catch (SQLException ex) {
Universal.get().debug(ex);
Universal.get().debugSqlException(ex);
}
}

Expand Down

0 comments on commit 396b2fb

Please sign in to comment.