From 4db1b5d2999827bc4de28574a6993e89f5967ec1 Mon Sep 17 00:00:00 2001 From: ACoolmanTelicent Date: Wed, 4 Dec 2024 14:29:19 +0000 Subject: [PATCH] fix(ApiTypeAheadResponseSchema): convert exception to console output --- src/hooks/useTypeahead.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/hooks/useTypeahead.ts b/src/hooks/useTypeahead.ts index 30197bd..dd29243 100644 --- a/src/hooks/useTypeahead.ts +++ b/src/hooks/useTypeahead.ts @@ -1,6 +1,7 @@ import { useQuery } from "@tanstack/react-query"; import { fetchOptions } from "./query-utils"; import { ApiTypeAheadResponseSchema } from "../schema/ApiTypeAheadResponseSchema"; +import { z, ZodError } from "zod"; const fetchSearchResults = async ( url: string, @@ -17,7 +18,17 @@ const fetchSearchResults = async ( `An error occured while retrieving search results for query ${query}` ); } - return ApiTypeAheadResponseSchema.parse(response.json()); + const json = response.json(); + try { + return ApiTypeAheadResponseSchema.parse(json); + } catch (error) { + if (error instanceof ZodError) { + console.warn(error); + } else { + console.error(error); + } + return json as unknown as z.infer; // Graceful degradation + } }; const useTypeaheadQuery = (