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