Skip to content

Commit 3d36dd7

Browse files
committed
chore: prevent internal errors
1 parent 07c920f commit 3d36dd7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

apps/site/next-data/downloadSnippets.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import {
77
import { availableLocaleCodes } from '@/next.locales.mjs';
88
import type { DownloadSnippet } from '@/types';
99

10+
// Prevents React from throwing an Error when not able to fulfil a request
11+
// due to internal processing errors
12+
const parseDownloadSnippetResponse = (data: string): Array<DownloadSnippet> =>
13+
data.startsWith('{') ? JSON.parse(data) : [];
14+
1015
const getDownloadSnippets = (lang: string): Promise<Array<DownloadSnippet>> => {
1116
// Prevents attempting to retrieve data for an unsupported language as both the generator
1217
// and the API endpoint will simply return 404. And we want to prevent a 404 response.
@@ -38,7 +43,7 @@ const getDownloadSnippets = (lang: string): Promise<Array<DownloadSnippet>> => {
3843
// that does not provide a clear stack trace of which request is failing and what the JSON.parse error is
3944
return fetch(fetchURL)
4045
.then(response => response.text())
41-
.then(JSON.parse);
46+
.then(response => parseDownloadSnippetResponse(response));
4247
};
4348

4449
export default getDownloadSnippets;

0 commit comments

Comments
 (0)