fix: improve error handling and type safety

- Replaced 'any' types with proper TypeScript types
- Improved error handling with instanceof checks
- Removed unused imports (calculateExpectedTeamScore)
- Updated error messages to be more descriptive
- Removed deprecated eslint.config.mjs file
This commit is contained in:
2026-03-29 21:20:40 -07:00
parent 576dfda619
commit c9415d490e
11 changed files with 30 additions and 54 deletions
+3 -2
View File
@@ -150,8 +150,9 @@ export default function NewTournamentPage() {
// Redirect to game entry page
router.push(`/admin/tournaments/${tournamentId}/entry`)
} catch (err: any) {
setError(err.message)
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "An unexpected error occurred";
setError(message)
} finally {
setIsLoading(false)
}
+9 -7
View File
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
import Papa from "papaparse";
import { canManageTournament } from "@/lib/permissions";
import { getSession } from "@/lib/auth-simple";
import { calculateTeamElo, calculateExpectedTeamScore, calculateTeamEloChange } from "@/lib/elo-utils";
import { calculateTeamElo, calculateTeamEloChange } from "@/lib/elo-utils";
interface CSVRow {
"Event #": string;
@@ -62,7 +62,7 @@ export async function POST(request: Request) {
if (parseResult.errors.length > 0) {
return NextResponse.json(
{ error: "CSV parsing errors", errors: parseResult.errors.map((e: any) => e.message) },
{ error: "CSV parsing errors", errors: parseResult.errors.map((e: { message: string }) => e.message) },
{ status: 400 }
);
}
@@ -164,15 +164,16 @@ export async function POST(request: Request) {
registrationDate: new Date(),
},
});
} catch (error) {
} catch {
// Participant already exists, which is fine
console.log(`Participant ${player.id} already exists in tournament ${eventId}`);
}
}
importedCount++;
} catch (err: any) {
errors.push(`Row ${index + 2}: ${err.message}`);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "Unknown error";
errors.push(`Row ${index + 2}: ${message}`);
errorCount++;
}
}
@@ -182,10 +183,11 @@ export async function POST(request: Request) {
errorCount,
errors: errors.length > 0 ? errors : undefined,
});
} catch (error: any) {
} catch (error: unknown) {
console.error("Upload error:", error);
const message = error instanceof Error ? error.message : "Failed to upload CSV";
return NextResponse.json(
{ error: error.message || "Failed to upload CSV" },
{ error: message },
{ status: 500 }
);
}
@@ -4,18 +4,7 @@ import { canManageTournament } from "@/lib/permissions";
import { getSession } from "@/lib/auth-simple";
import { calculateTeamElo, calculateTeamEloChange } from "@/lib/elo-utils";
interface GameEntry {
round: number
table: string
player1: string
player2: string
score1: number
player3: string
player4: string
score2: number
}
const K_FACTOR = 32;
const K_FACTOR = 32; // Standard K-factor for Elo calculations
export async function POST(
request: Request,
@@ -139,15 +128,16 @@ export async function POST(
},
});
}
} catch (error) {
} catch {
// Participant already exists, which is fine
console.log(`Participant ${player.id} already exists in tournament ${tournamentId}`);
}
}
importedCount++;
} catch (err: any) {
errors.push(`Game ${index + 1}: ${err.message}`);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "Unknown error";
errors.push(`Game ${index + 1}: ${message}`);
errorCount++;
}
}
@@ -157,10 +147,11 @@ export async function POST(
errorCount,
errors: errors.length > 0 ? errors : undefined,
});
} catch (error: any) {
} catch (error: unknown) {
console.error("Bulk game submission error:", error);
const message = error instanceof Error ? error.message : "Failed to submit games";
return NextResponse.json(
{ error: error.message || "Failed to submit games" },
{ error: message },
{ status: 500 }
);
}
+1 -1
View File
@@ -39,7 +39,7 @@ export async function GET(
}
return NextResponse.json({ role: user.role });
} catch (error) {
} catch {
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ export default function LoginPage() {
// Navigate to admin page - Better Auth will handle session update
router.push("/admin")
}
} catch (err) {
} catch {
setError("An unexpected error occurred")
} finally {
setLoading(false)
+1 -1
View File
@@ -66,7 +66,7 @@ export default function EditTournamentForm({ tournament }: EditTournamentFormPro
setTimeout(() => {
router.push(`/admin/tournaments/${tournament.id}`)
}, 2000)
} catch (err) {
} catch {
setError("An error occurred. Please try again.")
setIsLoading(false)
}
+2 -2
View File
@@ -20,7 +20,7 @@ interface MatchData {
table: string
}
export default function MatchEditor({ tournamentId, players, matches }: MatchEditorProps) {
export default function MatchEditor({ tournamentId, players }: MatchEditorProps) {
const [formData, setFormData] = useState<MatchData>({
team1P1Id: null,
team1P2Id: null,
@@ -120,7 +120,7 @@ export default function MatchEditor({ tournamentId, players, matches }: MatchEdi
setTimeout(() => {
window.location.reload()
}, 1000)
} catch (err) {
} catch {
setError("An error occurred. Please try again.")
setIsLoading(false)
}
+2 -2
View File
@@ -4,7 +4,7 @@ import { createContext, useContext, useEffect, useState } from "react"
import { authClient } from "@/lib/auth-client"
interface SessionContextType {
session: { user: any; session: any } | null
session: { user: unknown; session: unknown } | null
loading: boolean
refreshSession: () => Promise<void>
}
@@ -12,7 +12,7 @@ interface SessionContextType {
const SessionContext = createContext<SessionContextType | undefined>(undefined)
export function SessionProvider({ children }: { children: React.ReactNode }) {
const [session, setSession] = useState<{ user: any; session: any } | null>(null)
const [session, setSession] = useState<{ user: unknown; session: unknown } | null>(null)
const [loading, setLoading] = useState(true)
const refreshSession = async () => {
+2 -2
View File
@@ -45,11 +45,11 @@ export type AuthUser = {
email: string;
name?: string;
role?: string;
[key: string]: any;
[key: string]: unknown;
};
session: {
token: string;
expiresAt: Date;
[key: string]: any;
[key: string]: unknown;
};
} | null;
+1 -1
View File
@@ -16,7 +16,7 @@ declare module "papaparse" {
}
}
export function parse<T = any>(
export function parse<T = unknown>(
input: string,
config?: {
header?: boolean