Refactor code structure for improved readability and maintainability
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 53s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 53s
This commit is contained in:
@@ -268,6 +268,45 @@ app.use(express.urlencoded({ extended: true, verify: captureRawBody }));
|
||||
// Wichtig: userId wird später in authMiddleware gesetzt, aber Middleware funktioniert auch ohne
|
||||
app.use(requestLoggingMiddleware);
|
||||
|
||||
// Normalisierungs-Middleware: Wenn `drawingData` als Objekt ankommt, immer als JSON-String speichern.
|
||||
// Vermeidet Typ-Mismatches zwischen Client (Objekt) und DB/Code (stringified JSON).
|
||||
function normalizeDrawingDataInObject(obj) {
|
||||
if (!obj || typeof obj !== 'object') return;
|
||||
if (Array.isArray(obj)) return obj.forEach(normalizeDrawingDataInObject);
|
||||
|
||||
// Direkte Felder
|
||||
try {
|
||||
if (obj.drawingData && typeof obj.drawingData === 'object') {
|
||||
obj.drawingData = JSON.stringify(obj.drawingData);
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
// häufiges Pattern: predefinedActivity.drawingData
|
||||
try {
|
||||
if (obj.predefinedActivity && obj.predefinedActivity.drawingData && typeof obj.predefinedActivity.drawingData === 'object') {
|
||||
obj.predefinedActivity.drawingData = JSON.stringify(obj.predefinedActivity.drawingData);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
// Rekursiv durch alle Keys gehen
|
||||
for (const k of Object.keys(obj)) {
|
||||
try { normalizeDrawingDataInObject(obj[k]); } catch (e) {}
|
||||
}
|
||||
}
|
||||
|
||||
app.use((req, res, next) => {
|
||||
try {
|
||||
if (req.body) normalizeDrawingDataInObject(req.body);
|
||||
if (req.query) normalizeDrawingDataInObject(req.query);
|
||||
} catch (e) {
|
||||
// Never fail the request because of normalization
|
||||
console.error('[normalizeDrawingData] Error:', e && e.message ? e.message : e);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
// Globale Fehlerbehandlung, damit der Server bei unerwarteten Fehlern nicht hart abstürzt
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error('[uncaughtException]', err);
|
||||
|
||||
Reference in New Issue
Block a user