File tree Expand file tree Collapse file tree 1 file changed +6
-1
lines changed
Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ import {
77import { availableLocaleCodes } from '@/next.locales.mjs' ;
88import 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+
1015const 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
4449export default getDownloadSnippets ;
You can’t perform that action at this time.
0 commit comments