mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 06:12:20 +08:00
fix: only "swallow" the json() parsing error if the response is empty (#47493)
This commit is contained in:
@ -106,13 +106,15 @@ export async function parseResponseBody<T>(
|
||||
return response.blob() as any;
|
||||
|
||||
case 'json':
|
||||
try {
|
||||
return await response.json();
|
||||
} catch (err) {
|
||||
console.warn(`${response.url} returned an invalid JSON -`, err);
|
||||
// An empty string is not a valid JSON.
|
||||
// Sometimes (unfortunately) our APIs declare their Content-Type as JSON, however they return an empty body.
|
||||
if (response.headers.get('Content-Length') === '0') {
|
||||
console.warn(`${response.url} returned an invalid JSON`);
|
||||
return {} as unknown as T;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
|
||||
case 'text':
|
||||
return response.text() as any;
|
||||
}
|
||||
|
Reference in New Issue
Block a user