Add global error handling middleware for API routes and enhance rating update logic

Implemented a global error handling middleware in the server to standardize error responses for API routes. Additionally, updated the AutoUpdateRatingsService to retrieve the approved user club before updating ratings, ensuring proper access control. Modified the error handling in MyTischtennisAccount.vue to provide more informative login failure messages.
This commit is contained in:
Torsten Schulz (local)
2025-11-07 13:22:22 +01:00
parent eba8ba30aa
commit 94aab93f7d
3 changed files with 48 additions and 3 deletions

View File

@@ -71,6 +71,31 @@ 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;
const message = err?.message || 'Interner Serverfehler';
const response = {
success: false,
message,
error: message
};
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);