bonjr
bonjr
RRailway
Created by bonjr on 8/25/2023 in #✋|help
Deployment and logging issues (Maven)
Hello, My program is running fine locally, but when I deploy it with Railway, I start seeing issues. I think the issue is primarily with grabbing the environment or database variable.
private static Connection getConnection() throws SQLException {
// String dbUrl = toJdbcUrl(readFromFile("DATABASE_URL")); // local testing
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL")); // live

return DriverManager.getConnection(dbUrl);
}

private static String toJdbcUrl(String databaseUrl) {
if (databaseUrl == null || !databaseUrl.startsWith("postgresql://")) {
return null;
}

int protocolEnd = databaseUrl.indexOf("://");
int credentialsEnd = databaseUrl.lastIndexOf("@");

String credentials = databaseUrl.substring(protocolEnd + 3, credentialsEnd);
String[] splitCredentials = credentials.split(":");

String user = splitCredentials[0];
String password = splitCredentials[1];

String afterCredentials = databaseUrl.substring(credentialsEnd + 1);
String host = afterCredentials.substring(0, afterCredentials.indexOf(":"));
String portAndDatabase = afterCredentials.substring(afterCredentials.indexOf(":") + 1);

String jdbcUrl = "jdbc:postgresql://" + host + ":" + portAndDatabase + "?user=" + user + "&password="
+ password;

return jdbcUrl;
}
private static Connection getConnection() throws SQLException {
// String dbUrl = toJdbcUrl(readFromFile("DATABASE_URL")); // local testing
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL")); // live

return DriverManager.getConnection(dbUrl);
}

private static String toJdbcUrl(String databaseUrl) {
if (databaseUrl == null || !databaseUrl.startsWith("postgresql://")) {
return null;
}

int protocolEnd = databaseUrl.indexOf("://");
int credentialsEnd = databaseUrl.lastIndexOf("@");

String credentials = databaseUrl.substring(protocolEnd + 3, credentialsEnd);
String[] splitCredentials = credentials.split(":");

String user = splitCredentials[0];
String password = splitCredentials[1];

String afterCredentials = databaseUrl.substring(credentialsEnd + 1);
String host = afterCredentials.substring(0, afterCredentials.indexOf(":"));
String portAndDatabase = afterCredentials.substring(afterCredentials.indexOf(":") + 1);

String jdbcUrl = "jdbc:postgresql://" + host + ":" + portAndDatabase + "?user=" + user + "&password="
+ password;

return jdbcUrl;
}
The reason I think it's some database/environment issue is because the logs on Railway says:
Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)
Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)
So it would appear that this part from main function around the JDA part is having issues:
// bot
JDABuilder builder = JDABuilder.createDefault(Utility.readFromDatabase("TOKEN"));
// bot
JDABuilder builder = JDABuilder.createDefault(Utility.readFromDatabase("TOKEN"));
I have double checked that the TOKEN variable is the same locally and on the Postgres database, so the issue is not the token itself.
public static String readFromDatabase(String key) {
String sql = "SELECT value FROM config WHERE key = ?";

try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, key);

ResultSet rs = stmt.executeQuery();

if (rs.next()) {
return rs.getString("value");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}

return null;
}
public static String readFromDatabase(String key) {
String sql = "SELECT value FROM config WHERE key = ?";

try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, key);

ResultSet rs = stmt.executeQuery();

if (rs.next()) {
return rs.getString("value");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}

return null;
}
I can't seem to figure out what is wrong. And also, how can I use the deploy log to my advantage for debugging? Locally, sometimes i'll just do System.out.println to check variables, etc., but this does not show up in the deploy logs.
88 replies