Skip to content

fix: handle syntax error #8506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .astro/types.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference types="astro/client" />
/// <reference path="content.d.ts" />
28 changes: 18 additions & 10 deletions src/hooks/use-custom-roadmap.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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<GetRoadmapResponse>(roadmapUrl.toString());
} catch (error) {
if (error instanceof SyntaxError) {
throw new FetchError(404, 'Roadmap not found');
}
Comment on lines +37 to +40
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the check on status code instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanna do it, we will have to do it inside httpCall function.


return httpGet(roadmapUrl.toString());
throw error;
}
},
retry: false,
enabled: !!(slug || id),
Expand Down
17 changes: 12 additions & 5 deletions src/lib/query-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ type HttpOptionsType = RequestInit;

type AppResponse = Record<string, any>;

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 = {
Expand Down Expand Up @@ -69,10 +79,7 @@ export async function httpCall<ResponseType = AppResponse>(

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');
}
Expand Down