Centralize and standardize database error handling

Introduced a new handleDBError utility in modules/utils.js to centralize database error logging and throwing. Refactored all repositories to use this utility, replacing repetitive error handling and logger calls with a single function call for improved maintainability and consistency.
This commit is contained in:
2026-01-18 18:04:10 +01:00
parent 88f8ee57e1
commit 71627c7041
7 changed files with 91 additions and 183 deletions

View File

@@ -1,11 +1,14 @@
const fs = require("node:fs")
const path = require("node:path")
const utils = require("./utils")
require("colors")
require("dotenv").config({
quiet: true
})
function isTrueFromDotEnv(key) {
return (process.env[key] || "").trim().toLowerCase() === "true"
}
function cleanup($stream) {
if (!$stream.destroyed) {
$stream.end()
@@ -41,7 +44,7 @@ function write($stream, level, color, content, extraLabels = []) {
function createLogger(root) {
// eslint-disable-next-line no-useless-escape
const fileName = utils.isTrueFromDotEnv("IS_PROD") ? new Date().toLocaleString("fr-FR", { timeZone: "UTC" }).replace(/[\/:]/g, "-").replace(/ /g, "_") : "DEV-LOG"
const fileName = isTrueFromDotEnv("IS_PROD") ? new Date().toLocaleString("fr-FR", { timeZone: "UTC" }).replace(/[\/:]/g, "-").replace(/ /g, "_") : "DEV-LOG"
const logsDir = path.join(root, "logs")

View File

@@ -1,4 +1,6 @@
const crypto = require("node:crypto")
const logger = require("../modules/logger")
const { DefaultError } = require("../errors/errors")
const certificatesManager = require("./certificatesManager")
async function getRegistrationCountryFromIp(ipAddress) {
@@ -60,8 +62,14 @@ function getUrlParam(url, param) {
return urlParams.get(param)
}
function handleDBError(error, errorMessage = "Internal Server Error", code = 500) {
logger.log(errorMessage.bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(code, errorMessage, "InternalServerError")
}
module.exports = {
getUrlParam,
handleDBError,
signProfileData,
addDashesToUUID,
isTrueFromDotEnv,