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.
This commit is contained in:
Torsten Schulz (local)
2025-11-07 13:45:58 +01:00
parent 94aab93f7d
commit 498742e6ae
5 changed files with 23 additions and 23 deletions

View File

@@ -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({

View File

@@ -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' });

View File

@@ -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

View File

@@ -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);

View File

@@ -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 {