Skip to content

Commit

Permalink
fix(ApiTypeAheadResponseSchema): convert exception to console output
Browse files Browse the repository at this point in the history
  • Loading branch information
ACoolmanTelicent committed Dec 4, 2024
1 parent 589b6b5 commit 4db1b5d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/hooks/useTypeahead.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<typeof ApiTypeAheadResponseSchema>; // Graceful degradation
}
};

const useTypeaheadQuery = (
Expand Down

0 comments on commit 4db1b5d

Please # to comment.