Improve clarity of error message when database SSL disabled and file storage cannot be written

This commit is contained in:
Tom Moor
2023-09-27 09:07:49 -04:00
parent 2261514138
commit 86cb861ca7
3 changed files with 24 additions and 2 deletions

View File

@@ -12,7 +12,7 @@ export const createRootDirForLocalStorage = () => {
}
} catch (err) {
Logger.fatal(
"Couldn't create root dir for local storage of attachments",
`Failed to create directory for local file storage at ${env.FILE_STORAGE_LOCAL_ROOT_DIR}`,
err
);
}

View File

@@ -23,7 +23,7 @@ import { checkEnv, checkPendingMigrations } from "./utils/startup";
import { checkUpdates } from "./utils/updates";
import onerror from "./onerror";
import ShutdownHelper, { ShutdownOrder } from "./utils/ShutdownHelper";
import { sequelize } from "./storage/database";
import { checkConnection, sequelize } from "./storage/database";
import RedisAdapter from "./storage/redis";
import Metrics from "./logging/Metrics";
@@ -50,6 +50,7 @@ if (serviceNames.includes("collaboration")) {
// This function will only be called once in the original process
async function master() {
await checkConnection();
await checkEnv();
await checkPendingMigrations();

View File

@@ -5,6 +5,7 @@ import env from "@server/env";
import Logger from "../logging/Logger";
import * as models from "../models";
const isDevelopment = env.ENVIRONMENT === "development";
const isProduction = env.ENVIRONMENT === "production";
const isSSLDisabled = env.PGSSLMODE === "disable";
const poolMax = env.DATABASE_CONNECTION_POOL_MAX ?? 5;
@@ -18,6 +19,7 @@ export const sequelize = new Sequelize(url, {
logging: (msg) =>
process.env.DEBUG?.includes("database") && Logger.debug("database", msg),
typeValidation: true,
logQueryParameters: isDevelopment,
dialectOptions: {
ssl:
isProduction && !isSSLDisabled
@@ -36,6 +38,25 @@ export const sequelize = new Sequelize(url, {
},
});
/**
* This function is used to test the database connection on startup. It will
* throw a descriptive error if the connection fails.
*/
export const checkConnection = async () => {
try {
await sequelize.authenticate();
} catch (error) {
if (error.message.includes("does not support SSL")) {
Logger.fatal(
"The database does not support SSL connections. Set the `PGSSLMODE` environment variable to `disable` or enable SSL on your database server.",
error
);
} else {
Logger.fatal("Failed to connect to database", error);
}
}
};
export const migrations = new Umzug({
migrations: {
glob: ["migrations/*.js", { cwd: path.resolve("server") }],