From eb68c5d00b33998dc16e31f6993726d9e596222a Mon Sep 17 00:00:00 2001 From: vikingowl Date: Wed, 12 Nov 2025 23:14:19 +0100 Subject: [PATCH] fix: Add safe parsing for weapons and chat API endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use parseMatchWeaponsSafe instead of parseMatchWeapons - Use parseMatchChatSafe instead of parseMatchChat - Throw clear error message when demo not parsed yet - Prevents Zod validation errors from reaching page loaders - Matches the pattern already used for rounds endpoint This fixes Zod errors when navigating to match detail pages where the demo hasn't been fully parsed yet by the backend. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/lib/api/matches.ts | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/lib/api/matches.ts b/src/lib/api/matches.ts index e42d932..3f99c1f 100644 --- a/src/lib/api/matches.ts +++ b/src/lib/api/matches.ts @@ -1,8 +1,8 @@ import { apiClient } from './client'; import { parseMatchRoundsSafe, - parseMatchWeapons, - parseMatchChat, + parseMatchWeaponsSafe, + parseMatchChatSafe, parseMatchParseResponse } from '$lib/schemas'; import { @@ -56,13 +56,22 @@ export const matchesAPI = { * Get match weapons statistics * @param matchId - Match ID * @returns Weapon statistics for all players + * @throws Error if data is invalid or demo not parsed yet */ async getMatchWeapons(matchId: string | number): Promise { const url = `/match/${matchId}/weapons`; - const data = await apiClient.get(url); + const data = await apiClient.get(url); - // Validate with Zod schema - return parseMatchWeapons(data); + // Validate with Zod schema using safe parse + // This handles cases where the demo hasn't been parsed yet + const result = parseMatchWeaponsSafe(data); + + if (!result.success) { + // If validation fails, it's likely the demo hasn't been parsed yet + throw new Error('Demo not parsed yet or invalid response format'); + } + + return result.data; }, /** @@ -91,13 +100,22 @@ export const matchesAPI = { * Get match chat messages * @param matchId - Match ID * @returns Chat messages from the match + * @throws Error if data is invalid or demo not parsed yet */ async getMatchChat(matchId: string | number): Promise { const url = `/match/${matchId}/chat`; - const data = await apiClient.get(url); + const data = await apiClient.get(url); - // Validate with Zod schema - return parseMatchChat(data); + // Validate with Zod schema using safe parse + // This handles cases where the demo hasn't been parsed yet + const result = parseMatchChatSafe(data); + + if (!result.success) { + // If validation fails, it's likely the demo hasn't been parsed yet + throw new Error('Demo not parsed yet or invalid response format'); + } + + return result.data; }, /**