Refactor global error handling in server and improve logging in myTischtennisService

This commit moves the global error handling middleware in server.js to ensure it is applied after all routes. It also enhances the error handling in myTischtennisService by adding detailed logging for login attempts and failures, improving the visibility of issues during the login process. Additionally, the decryptData function in encrypt.js is updated to check if data is already decrypted, enhancing its robustness. Frontend changes include a minor adjustment to the button type in MyTischtennisAccount.vue for better accessibility.
This commit is contained in:
Torsten Schulz (local)
2025-11-21 09:17:48 +01:00
parent 0525f7908d
commit 51e47cf9f9
4 changed files with 67 additions and 43 deletions

View File

@@ -80,44 +80,6 @@ process.on('unhandledRejection', (reason, promise) => {
console.error('[unhandledRejection]', reason);
});
// Globale Fehlerbehandlung für API-Routen
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
const status = err?.statusCode || err?.status || 500;
// Unterstützung für Fehlercodes
let errorResponse;
if (err instanceof HttpError && err.errorCode) {
// Neues Format mit Fehlercode
errorResponse = err.toJSON();
} else {
// Legacy-Format: String-Nachricht
const message = err?.message || 'Interner Serverfehler';
errorResponse = {
message
};
}
const response = {
success: false,
...errorResponse,
// Für Rückwärtskompatibilität: error-Feld mit Nachricht
error: errorResponse.message || errorResponse.code || 'Interner Serverfehler'
};
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'development') {
response.debug = {
stack: err?.stack || null
};
}
console.error('[ExpressError]', err);
res.status(status).json(response);
});
app.use('/api/auth', authRoutes);
app.use('/api/clubs', clubRoutes);
app.use('/api/clubmembers', memberRoutes);
@@ -196,6 +158,44 @@ const setCanonicalTag = (req, res, next) => {
app.use(setCanonicalTag);
app.use(express.static(path.join(__dirname, '../frontend/dist')));
// Globale Fehlerbehandlung für API-Routen (MUSS nach allen Routes sein!)
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
const status = err?.statusCode || err?.status || 500;
// Unterstützung für Fehlercodes
let errorResponse;
if (err instanceof HttpError && err.errorCode) {
// Neues Format mit Fehlercode
errorResponse = err.toJSON();
} else {
// Legacy-Format: String-Nachricht
const message = err?.message || 'Interner Serverfehler';
errorResponse = {
message
};
}
const response = {
success: false,
...errorResponse,
// Für Rückwärtskompatibilität: error-Feld mit Nachricht
error: errorResponse.message || errorResponse.code || 'Interner Serverfehler'
};
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'development') {
response.debug = {
stack: err?.stack || null
};
}
console.error('[ExpressError]', err);
res.status(status).json(response);
});
(async () => {
try {
await sequelize.authenticate();