Refactor MyTischtennisUrlController and enhance error handling in TeamManagementView
Refactored MyTischtennisUrlController to define variables outside of try/catch for better error handling. Improved error messaging in TeamManagementView by providing more detailed debug information and ensuring fallback messages are available. This enhances the robustness of data retrieval and user feedback.
This commit is contained in:
@@ -219,6 +219,11 @@ class MyTischtennisUrlController {
|
||||
* Body: { clubTeamId: number }
|
||||
*/
|
||||
async fetchTeamData(req, res, next) {
|
||||
// Define outside of try/catch so catch has access
|
||||
let account = null;
|
||||
let team = null;
|
||||
let myTischtennisUrl = null;
|
||||
let requestStartTime = null;
|
||||
try {
|
||||
const { clubTeamId } = req.body;
|
||||
const userIdOrEmail = req.headers.userid;
|
||||
@@ -269,7 +274,7 @@ class MyTischtennisUrlController {
|
||||
}
|
||||
|
||||
// Get account data (for clubId, etc.)
|
||||
const account = await myTischtennisService.getAccount(userId);
|
||||
account = await myTischtennisService.getAccount(userId);
|
||||
|
||||
if (!account) {
|
||||
throw new HttpError(404, 'MyTischtennis-Account nicht verknüpft. Bitte verknüpfen Sie Ihren Account in den MyTischtennis-Einstellungen.');
|
||||
@@ -277,7 +282,7 @@ class MyTischtennisUrlController {
|
||||
|
||||
|
||||
// Get team with league and season
|
||||
const team = await ClubTeam.findByPk(clubTeamId, {
|
||||
team = await ClubTeam.findByPk(clubTeamId, {
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
@@ -322,11 +327,11 @@ class MyTischtennisUrlController {
|
||||
: seasonFull;
|
||||
const seasonStr = seasonShort.replace('/', '--');
|
||||
const teamnameEncoded = encodeURIComponent(team.name.replace(/\s/g, '_'));
|
||||
const myTischtennisUrl = `https://www.mytischtennis.de/click-tt/${team.league.association}/${seasonStr}/ligen/${team.league.groupname}/gruppe/${team.league.myTischtennisGroupId}/mannschaft/${team.myTischtennisTeamId}/${teamnameEncoded}/spielerbilanzen/gesamt`;
|
||||
myTischtennisUrl = `https://www.mytischtennis.de/click-tt/${team.league.association}/${seasonStr}/ligen/${team.league.groupname}/gruppe/${team.league.myTischtennisGroupId}/mannschaft/${team.myTischtennisTeamId}/${teamnameEncoded}/spielerbilanzen/gesamt`;
|
||||
|
||||
// Log the request to myTischtennis BEFORE making the call
|
||||
// This ensures we always see what WILL BE sent, even if the call fails
|
||||
const requestStartTime = Date.now();
|
||||
requestStartTime = Date.now();
|
||||
try {
|
||||
await apiLogService.logRequest({
|
||||
userId: account.userId,
|
||||
@@ -390,8 +395,8 @@ class MyTischtennisUrlController {
|
||||
} catch (error) {
|
||||
|
||||
// Update log with error information if we got far enough to build the URL
|
||||
if (typeof myTischtennisUrl !== 'undefined' && account && team) {
|
||||
const requestExecutionTime = Date.now() - requestStartTime;
|
||||
if (myTischtennisUrl && account && team) {
|
||||
const requestExecutionTime = requestStartTime ? (Date.now() - requestStartTime) : null;
|
||||
try {
|
||||
await apiLogService.logRequest({
|
||||
userId: account.userId,
|
||||
|
||||
Reference in New Issue
Block a user