From 74267a60613dac4d797deef4d45247ccc0fbf5e5 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 30 Apr 2025 21:32:12 +0600 Subject: [PATCH] fix: handle syntax error (#8506) --- .astro/types.d.ts | 1 + src/hooks/use-custom-roadmap.ts | 28 ++++++++++++++++++---------- src/lib/query-http.ts | 17 ++++++++++++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.astro/types.d.ts b/.astro/types.d.ts index f964fe0cf..03d7cc43f 100644 --- a/.astro/types.d.ts +++ b/.astro/types.d.ts @@ -1 +1,2 @@ /// +/// \ No newline at end of file diff --git a/src/hooks/use-custom-roadmap.ts b/src/hooks/use-custom-roadmap.ts index 2a9858835..77a996b5d 100644 --- a/src/hooks/use-custom-roadmap.ts +++ b/src/hooks/use-custom-roadmap.ts @@ -1,6 +1,6 @@ import { useQuery } from '@tanstack/react-query'; import type { GetRoadmapResponse } from '../components/CustomRoadmap/CustomRoadmap'; -import { httpGet, type FetchError } from '../lib/query-http'; +import { httpGet, FetchError } from '../lib/query-http'; import { queryClient } from '../stores/query-client'; type UseCustomRoadmapOptions = { @@ -22,17 +22,25 @@ export function useCustomRoadmap(options: UseCustomRoadmapOptions) { }, ], queryFn: async () => { - const roadmapUrl = slug - ? new URL( - `${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap-by-slug/${slug}`, - ) - : new URL(`${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap/${id}`); + try { + const roadmapUrl = slug + ? new URL( + `${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap-by-slug/${slug}`, + ) + : new URL(`${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap/${id}`); - if (secret) { - roadmapUrl.searchParams.set('secret', secret); - } + if (secret) { + roadmapUrl.searchParams.set('secret', secret); + } + + return await httpGet(roadmapUrl.toString()); + } catch (error) { + if (error instanceof SyntaxError) { + throw new FetchError(404, 'Roadmap not found'); + } - return httpGet(roadmapUrl.toString()); + throw error; + } }, retry: false, enabled: !!(slug || id), diff --git a/src/lib/query-http.ts b/src/lib/query-http.ts index 64ba4db7c..48737bb64 100644 --- a/src/lib/query-http.ts +++ b/src/lib/query-http.ts @@ -6,9 +6,19 @@ type HttpOptionsType = RequestInit; type AppResponse = Record; -export interface FetchError extends Error { +export class FetchError extends Error { status: number; message: string; + + constructor(status: number, message: string) { + super(message); + this.status = status; + this.message = message; + } + + static isFetchError(error: any): error is FetchError { + return error instanceof FetchError; + } } type AppError = { @@ -69,10 +79,7 @@ export async function httpCall( if (!response.ok) { if (data.errors) { - const error = new Error() as FetchError; - error.message = data.message; - error.status = response?.status; - throw error; + throw new FetchError(response?.status, data.message); } else { throw new Error('An unexpected error occurred'); }