Skip to content

Commit

Permalink
fixup! Too big error message JabRef#4827
Browse files Browse the repository at this point in the history
  • Loading branch information
dpolishc committed May 12, 2019
1 parent cf514d0 commit aabd174
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/main/java/org/jabref/logic/shared/DBMSConnection.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.jabref.logic.shared;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException;
import org.jabref.model.database.shared.DBMSType;
import org.jabref.model.database.shared.DatabaseConnection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

public class DBMSConnection implements DatabaseConnection {

private static final Logger LOGGER = LoggerFactory.getLogger(DBMSConnection.class);
Expand All @@ -39,8 +38,8 @@ public DBMSConnection(DBMSConnectionProperties connectionProperties) throws SQLE
// Some systems like PostgreSQL retrieves 0 to every exception.
// Therefore a stable error determination is not possible.
LOGGER.error("Could not connect to database: " + e.getMessage() + " - Error code: " + e.getErrorCode());

throw e;
StringBuilder formattedErrorMessage = formatErrorMessage(e);
throw new SQLException(formattedErrorMessage.toString(), e);
}
}

Expand Down Expand Up @@ -71,4 +70,19 @@ public static Set<DBMSType> getAvailableDBMSTypes() {
}
return dbmsTypes;
}

private StringBuilder formatErrorMessage(SQLException e) {
String message = e.getMessage();
StringBuilder errorFormatter = new StringBuilder();
int wordCounter = 0;
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == ' ' && ++wordCounter % 25 == 0) {
errorFormatter.append(" ").append(System.lineSeparator());

continue;
}
errorFormatter.append(message.charAt(i));
}
return errorFormatter;
}
}

0 comments on commit aabd174

Please sign in to comment.