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:
@@ -1,18 +0,0 @@
|
|||||||
import { defineConfig, globalIgnores } from "eslint/config";
|
|
||||||
import nextVitals from "eslint-config-next/core-web-vitals";
|
|
||||||
import nextTs from "eslint-config-next/typescript";
|
|
||||||
|
|
||||||
const eslintConfig = defineConfig([
|
|
||||||
...nextVitals,
|
|
||||||
...nextTs,
|
|
||||||
// Override default ignores of eslint-config-next.
|
|
||||||
globalIgnores([
|
|
||||||
// Default ignores of eslint-config-next:
|
|
||||||
".next/**",
|
|
||||||
"out/**",
|
|
||||||
"build/**",
|
|
||||||
"next-env.d.ts",
|
|
||||||
]),
|
|
||||||
]);
|
|
||||||
|
|
||||||
export default eslintConfig;
|
|
||||||
@@ -150,8 +150,9 @@ export default function NewTournamentPage() {
|
|||||||
|
|
||||||
// Redirect to game entry page
|
// Redirect to game entry page
|
||||||
router.push(`/admin/tournaments/${tournamentId}/entry`)
|
router.push(`/admin/tournaments/${tournamentId}/entry`)
|
||||||
} catch (err: any) {
|
} catch (err: unknown) {
|
||||||
setError(err.message)
|
const message = err instanceof Error ? err.message : "An unexpected error occurred";
|
||||||
|
setError(message)
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
|
|||||||
import Papa from "papaparse";
|
import Papa from "papaparse";
|
||||||
import { canManageTournament } from "@/lib/permissions";
|
import { canManageTournament } from "@/lib/permissions";
|
||||||
import { getSession } from "@/lib/auth-simple";
|
import { getSession } from "@/lib/auth-simple";
|
||||||
import { calculateTeamElo, calculateExpectedTeamScore, calculateTeamEloChange } from "@/lib/elo-utils";
|
import { calculateTeamElo, calculateTeamEloChange } from "@/lib/elo-utils";
|
||||||
|
|
||||||
interface CSVRow {
|
interface CSVRow {
|
||||||
"Event #": string;
|
"Event #": string;
|
||||||
@@ -62,7 +62,7 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
if (parseResult.errors.length > 0) {
|
if (parseResult.errors.length > 0) {
|
||||||
return NextResponse.json(
|
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 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -164,15 +164,16 @@ export async function POST(request: Request) {
|
|||||||
registrationDate: new Date(),
|
registrationDate: new Date(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch {
|
||||||
// Participant already exists, which is fine
|
// Participant already exists, which is fine
|
||||||
console.log(`Participant ${player.id} already exists in tournament ${eventId}`);
|
console.log(`Participant ${player.id} already exists in tournament ${eventId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
importedCount++;
|
importedCount++;
|
||||||
} catch (err: any) {
|
} catch (err: unknown) {
|
||||||
errors.push(`Row ${index + 2}: ${err.message}`);
|
const message = err instanceof Error ? err.message : "Unknown error";
|
||||||
|
errors.push(`Row ${index + 2}: ${message}`);
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,10 +183,11 @@ export async function POST(request: Request) {
|
|||||||
errorCount,
|
errorCount,
|
||||||
errors: errors.length > 0 ? errors : undefined,
|
errors: errors.length > 0 ? errors : undefined,
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
console.error("Upload error:", error);
|
console.error("Upload error:", error);
|
||||||
|
const message = error instanceof Error ? error.message : "Failed to upload CSV";
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: error.message || "Failed to upload CSV" },
|
{ error: message },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,7 @@ import { canManageTournament } from "@/lib/permissions";
|
|||||||
import { getSession } from "@/lib/auth-simple";
|
import { getSession } from "@/lib/auth-simple";
|
||||||
import { calculateTeamElo, calculateTeamEloChange } from "@/lib/elo-utils";
|
import { calculateTeamElo, calculateTeamEloChange } from "@/lib/elo-utils";
|
||||||
|
|
||||||
interface GameEntry {
|
const K_FACTOR = 32; // Standard K-factor for Elo calculations
|
||||||
round: number
|
|
||||||
table: string
|
|
||||||
player1: string
|
|
||||||
player2: string
|
|
||||||
score1: number
|
|
||||||
player3: string
|
|
||||||
player4: string
|
|
||||||
score2: number
|
|
||||||
}
|
|
||||||
|
|
||||||
const K_FACTOR = 32;
|
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
request: Request,
|
request: Request,
|
||||||
@@ -139,15 +128,16 @@ export async function POST(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
// Participant already exists, which is fine
|
// Participant already exists, which is fine
|
||||||
console.log(`Participant ${player.id} already exists in tournament ${tournamentId}`);
|
console.log(`Participant ${player.id} already exists in tournament ${tournamentId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
importedCount++;
|
importedCount++;
|
||||||
} catch (err: any) {
|
} catch (err: unknown) {
|
||||||
errors.push(`Game ${index + 1}: ${err.message}`);
|
const message = err instanceof Error ? err.message : "Unknown error";
|
||||||
|
errors.push(`Game ${index + 1}: ${message}`);
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,10 +147,11 @@ export async function POST(
|
|||||||
errorCount,
|
errorCount,
|
||||||
errors: errors.length > 0 ? errors : undefined,
|
errors: errors.length > 0 ? errors : undefined,
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
console.error("Bulk game submission error:", error);
|
console.error("Bulk game submission error:", error);
|
||||||
|
const message = error instanceof Error ? error.message : "Failed to submit games";
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: error.message || "Failed to submit games" },
|
{ error: message },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export async function GET(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ role: user.role });
|
return NextResponse.json({ role: user.role });
|
||||||
} catch (error) {
|
} catch {
|
||||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export default function LoginPage() {
|
|||||||
// Navigate to admin page - Better Auth will handle session update
|
// Navigate to admin page - Better Auth will handle session update
|
||||||
router.push("/admin")
|
router.push("/admin")
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch {
|
||||||
setError("An unexpected error occurred")
|
setError("An unexpected error occurred")
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ export default function EditTournamentForm({ tournament }: EditTournamentFormPro
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
router.push(`/admin/tournaments/${tournament.id}`)
|
router.push(`/admin/tournaments/${tournament.id}`)
|
||||||
}, 2000)
|
}, 2000)
|
||||||
} catch (err) {
|
} catch {
|
||||||
setError("An error occurred. Please try again.")
|
setError("An error occurred. Please try again.")
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ interface MatchData {
|
|||||||
table: string
|
table: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MatchEditor({ tournamentId, players, matches }: MatchEditorProps) {
|
export default function MatchEditor({ tournamentId, players }: MatchEditorProps) {
|
||||||
const [formData, setFormData] = useState<MatchData>({
|
const [formData, setFormData] = useState<MatchData>({
|
||||||
team1P1Id: null,
|
team1P1Id: null,
|
||||||
team1P2Id: null,
|
team1P2Id: null,
|
||||||
@@ -120,7 +120,7 @@ export default function MatchEditor({ tournamentId, players, matches }: MatchEdi
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
}, 1000)
|
}, 1000)
|
||||||
} catch (err) {
|
} catch {
|
||||||
setError("An error occurred. Please try again.")
|
setError("An error occurred. Please try again.")
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { createContext, useContext, useEffect, useState } from "react"
|
|||||||
import { authClient } from "@/lib/auth-client"
|
import { authClient } from "@/lib/auth-client"
|
||||||
|
|
||||||
interface SessionContextType {
|
interface SessionContextType {
|
||||||
session: { user: any; session: any } | null
|
session: { user: unknown; session: unknown } | null
|
||||||
loading: boolean
|
loading: boolean
|
||||||
refreshSession: () => Promise<void>
|
refreshSession: () => Promise<void>
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ interface SessionContextType {
|
|||||||
const SessionContext = createContext<SessionContextType | undefined>(undefined)
|
const SessionContext = createContext<SessionContextType | undefined>(undefined)
|
||||||
|
|
||||||
export function SessionProvider({ children }: { children: React.ReactNode }) {
|
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 [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
const refreshSession = async () => {
|
const refreshSession = async () => {
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ export type AuthUser = {
|
|||||||
email: string;
|
email: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
role?: string;
|
role?: string;
|
||||||
[key: string]: any;
|
[key: string]: unknown;
|
||||||
};
|
};
|
||||||
session: {
|
session: {
|
||||||
token: string;
|
token: string;
|
||||||
expiresAt: Date;
|
expiresAt: Date;
|
||||||
[key: string]: any;
|
[key: string]: unknown;
|
||||||
};
|
};
|
||||||
} | null;
|
} | null;
|
||||||
|
|||||||
Vendored
+1
-1
@@ -16,7 +16,7 @@ declare module "papaparse" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parse<T = any>(
|
export function parse<T = unknown>(
|
||||||
input: string,
|
input: string,
|
||||||
config?: {
|
config?: {
|
||||||
header?: boolean
|
header?: boolean
|
||||||
|
|||||||
Reference in New Issue
Block a user