Files
tkmind_go/ui/desktop/src/utils/jsonUtils.ts
T
Douwe Osinga 4926e3579f Catch json errors a little better (#3437)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
2025-07-20 15:16:27 +02:00

14 lines
312 B
TypeScript

export async function safeJsonParse<T>(
response: Response,
errorMessage: string = 'Failed to parse server response'
): Promise<T> {
try {
return (await response.json()) as T;
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(errorMessage);
}
throw error;
}
}