mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-03 22:32:35 +02:00
fix: handle syntax error (#8506)
This commit is contained in:
1
.astro/types.d.ts
vendored
1
.astro/types.d.ts
vendored
@@ -1 +1,2 @@
|
|||||||
/// <reference types="astro/client" />
|
/// <reference types="astro/client" />
|
||||||
|
/// <reference path="content.d.ts" />
|
@@ -1,6 +1,6 @@
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import type { GetRoadmapResponse } from '../components/CustomRoadmap/CustomRoadmap';
|
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';
|
import { queryClient } from '../stores/query-client';
|
||||||
|
|
||||||
type UseCustomRoadmapOptions = {
|
type UseCustomRoadmapOptions = {
|
||||||
@@ -22,6 +22,7 @@ export function useCustomRoadmap(options: UseCustomRoadmapOptions) {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
|
try {
|
||||||
const roadmapUrl = slug
|
const roadmapUrl = slug
|
||||||
? new URL(
|
? new URL(
|
||||||
`${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap-by-slug/${slug}`,
|
`${import.meta.env.PUBLIC_API_URL}/v1-get-roadmap-by-slug/${slug}`,
|
||||||
@@ -32,7 +33,14 @@ export function useCustomRoadmap(options: UseCustomRoadmapOptions) {
|
|||||||
roadmapUrl.searchParams.set('secret', secret);
|
roadmapUrl.searchParams.set('secret', secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpGet(roadmapUrl.toString());
|
return await httpGet<GetRoadmapResponse>(roadmapUrl.toString());
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof SyntaxError) {
|
||||||
|
throw new FetchError(404, 'Roadmap not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
retry: false,
|
retry: false,
|
||||||
enabled: !!(slug || id),
|
enabled: !!(slug || id),
|
||||||
|
@@ -6,9 +6,19 @@ type HttpOptionsType = RequestInit;
|
|||||||
|
|
||||||
type AppResponse = Record<string, any>;
|
type AppResponse = Record<string, any>;
|
||||||
|
|
||||||
export interface FetchError extends Error {
|
export class FetchError extends Error {
|
||||||
status: number;
|
status: number;
|
||||||
message: string;
|
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 = {
|
type AppError = {
|
||||||
@@ -69,10 +79,7 @@ export async function httpCall<ResponseType = AppResponse>(
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
if (data.errors) {
|
if (data.errors) {
|
||||||
const error = new Error() as FetchError;
|
throw new FetchError(response?.status, data.message);
|
||||||
error.message = data.message;
|
|
||||||
error.status = response?.status;
|
|
||||||
throw error;
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('An unexpected error occurred');
|
throw new Error('An unexpected error occurred');
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user