From 498742e6aedf70a331efd26b7eeffda07ff5c23e Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 7 Nov 2025 13:45:58 +0100 Subject: [PATCH] Refactor error handling in controllers and services to standardize HttpError instantiation Updated multiple controllers and services to ensure consistent error handling by modifying the HttpError instantiation format. This change enhances clarity and maintains uniformity across error responses, improving overall code quality and maintainability. --- backend/controllers/apiLogController.js | 2 +- .../controllers/myTischtennisController.js | 6 +++--- .../controllers/myTischtennisUrlController.js | 20 +++++++++---------- backend/services/matchService.js | 2 +- backend/services/myTischtennisService.js | 16 +++++++-------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/backend/controllers/apiLogController.js b/backend/controllers/apiLogController.js index af81323..86713df 100644 --- a/backend/controllers/apiLogController.js +++ b/backend/controllers/apiLogController.js @@ -51,7 +51,7 @@ class ApiLogController { const log = await apiLogService.getLogById(parseInt(id)); if (!log) { - throw new HttpError(404, 'Log entry not found'); + throw new HttpError('Log entry not found', 404); } res.json({ diff --git a/backend/controllers/myTischtennisController.js b/backend/controllers/myTischtennisController.js index 0a298be..7628aed 100644 --- a/backend/controllers/myTischtennisController.js +++ b/backend/controllers/myTischtennisController.js @@ -45,12 +45,12 @@ class MyTischtennisController { const { email, password, savePassword, autoUpdateRatings, userPassword } = req.body; if (!email) { - throw new HttpError(400, 'E-Mail-Adresse erforderlich'); + throw new HttpError('E-Mail-Adresse erforderlich', 400); } // Wenn ein Passwort gesetzt wird, muss das App-Passwort angegeben werden if (password && !userPassword) { - throw new HttpError(400, 'App-Passwort erforderlich zum Setzen des myTischtennis-Passworts'); + throw new HttpError('App-Passwort erforderlich zum Setzen des myTischtennis-Passworts', 400); } const account = await myTischtennisService.upsertAccount( @@ -81,7 +81,7 @@ class MyTischtennisController { const deleted = await myTischtennisService.deleteAccount(userId); if (!deleted) { - throw new HttpError(404, 'Kein myTischtennis-Account gefunden'); + throw new HttpError('Kein myTischtennis-Account gefunden', 404); } res.status(200).json({ message: 'myTischtennis-Account gelöscht' }); diff --git a/backend/controllers/myTischtennisUrlController.js b/backend/controllers/myTischtennisUrlController.js index 33db5e2..9ba282d 100644 --- a/backend/controllers/myTischtennisUrlController.js +++ b/backend/controllers/myTischtennisUrlController.js @@ -21,7 +21,7 @@ class MyTischtennisUrlController { const { url } = req.body; if (!url) { - throw new HttpError('URL is required', 400); + throw new HttpError('URL and clubTeamId are required', 400); } // Validate URL @@ -79,7 +79,7 @@ class MyTischtennisUrlController { const userIdOrEmail = req.headers.userid; if (!url || !clubTeamId) { - throw new HttpError(400, 'URL and clubTeamId are required'); + throw new HttpError('URL and clubTeamId are required', 400); } // Get actual user ID @@ -87,7 +87,7 @@ class MyTischtennisUrlController { if (isNaN(userIdOrEmail)) { const user = await User.findOne({ where: { email: userIdOrEmail } }); if (!user) { - throw new HttpError(404, 'User not found'); + throw new HttpError('User not found', 404); } userId = user.id; } @@ -122,13 +122,13 @@ class MyTischtennisUrlController { } if (!season) { - throw new HttpError(404, `Season ${completeData.season} not found. Set createSeason=true to create it.`); + throw new HttpError(`Season ${completeData.season} not found. Set createSeason=true to create it.`, 404); } // Find or create league const team = await ClubTeam.findByPk(clubTeamId); if (!team) { - throw new HttpError(404, 'Club team not found'); + throw new HttpError('Club team not found', 404); } let league; @@ -176,7 +176,7 @@ class MyTischtennisUrlController { groupname: completeData.groupname }); } else { - throw new HttpError(400, 'League not found and team has no league assigned. Set createLeague=true to create one.'); + throw new HttpError('League not found and team has no league assigned. Set createLeague=true to create one.', 400); } // Update team @@ -243,7 +243,7 @@ class MyTischtennisUrlController { // It's an email, find the user const user = await User.findOne({ where: { email: userIdOrEmail } }); if (!user) { - throw new HttpError(404, 'User not found'); + throw new HttpError('User not found', 404); } userId = user.id; } @@ -489,11 +489,11 @@ class MyTischtennisUrlController { }); if (!team) { - throw new HttpError(404, 'Team not found'); + throw new HttpError('Team not found', 404); } if (!team.myTischtennisTeamId || !team.league || !team.league.myTischtennisGroupId) { - throw new HttpError(400, 'Team is not configured for myTischtennis'); + throw new HttpError('Team is not configured for myTischtennis', 400); } const url = myTischtennisUrlParserService.buildUrl({ @@ -525,7 +525,7 @@ class MyTischtennisUrlController { const userIdOrEmail = req.headers.userid; if (!url) { - throw new HttpError(400, 'URL is required'); + throw new HttpError('URL is required', 400); } // Parse URL diff --git a/backend/services/matchService.js b/backend/services/matchService.js index 2d32001..e0405ce 100644 --- a/backend/services/matchService.js +++ b/backend/services/matchService.js @@ -438,7 +438,7 @@ class MatchService { }); if (!match) { - throw new HttpError(404, 'Match not found'); + throw new HttpError('Match not found', 404); } await checkAccess(userToken, match.clubId); diff --git a/backend/services/myTischtennisService.js b/backend/services/myTischtennisService.js index 07a211b..127e445 100644 --- a/backend/services/myTischtennisService.js +++ b/backend/services/myTischtennisService.js @@ -24,7 +24,7 @@ class MyTischtennisService { // Verify user's app password const user = await User.findByPk(userId); if (!user) { - throw new HttpError(404, 'Benutzer nicht gefunden'); + throw new HttpError('Benutzer nicht gefunden', 404); } let loginResult = null; @@ -33,13 +33,13 @@ class MyTischtennisService { if (password) { const isValidPassword = await user.validatePassword(userPassword); if (!isValidPassword) { - throw new HttpError(401, 'Ungültiges Passwort'); + throw new HttpError('Ungültiges Passwort', 401); } // Login-Versuch bei myTischtennis loginResult = await myTischtennisClient.login(email, password); if (!loginResult.success) { - throw new HttpError(401, loginResult.error || 'myTischtennis-Login fehlgeschlagen. Bitte überprüfen Sie Ihre Zugangsdaten.'); + throw new HttpError(loginResult.error || 'myTischtennis-Login fehlgeschlagen. Bitte überprüfen Sie Ihre Zugangsdaten.', 401); } } @@ -147,7 +147,7 @@ class MyTischtennisService { const account = await MyTischtennis.findOne({ where: { userId } }); if (!account) { - throw new HttpError(404, 'Kein myTischtennis-Account verknüpft'); + throw new HttpError('Kein myTischtennis-Account verknüpft', 404); } let password = providedPassword; @@ -155,7 +155,7 @@ class MyTischtennisService { // Wenn kein Passwort übergeben wurde, versuche gespeichertes Passwort zu verwenden if (!password) { if (!account.savePassword || !account.encryptedPassword) { - throw new HttpError(400, 'Kein Passwort gespeichert. Bitte geben Sie Ihr Passwort ein.'); + throw new HttpError('Kein Passwort gespeichert. Bitte geben Sie Ihr Passwort ein.', 400); } password = account.getPassword(); } @@ -197,7 +197,7 @@ class MyTischtennisService { }; } else { await account.save(); // Save lastLoginAttempt - throw new HttpError(401, loginResult.error || 'myTischtennis-Login fehlgeschlagen'); + throw new HttpError(loginResult.error || 'myTischtennis-Login fehlgeschlagen', 401); } } @@ -224,12 +224,12 @@ class MyTischtennisService { const account = await MyTischtennis.findOne({ where: { userId } }); if (!account) { - throw new HttpError(404, 'Kein myTischtennis-Account verknüpft'); + throw new HttpError('Kein myTischtennis-Account verknüpft', 404); } // Check if session is valid if (!account.accessToken || !account.expiresAt || account.expiresAt < Date.now() / 1000) { - throw new HttpError(401, 'Session abgelaufen. Bitte erneut einloggen.'); + throw new HttpError('Session abgelaufen. Bitte erneut einloggen.', 401); } return {