Skip to content

Commit

Permalink
fix: handle case of text when invalid JSON (usebruno#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoD02 committed Sep 17, 2024
1 parent 572c7ea commit 8175e3f
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ const formatResponse = (data, mode, filter) => {
return '';
}

if (data === null) {
return data;
}

if (mode.includes('json')) {
let isValidJSON = false;

try {
isValidJSON = typeof JSON.parse(JSON.stringify(data)) === 'object';
} catch (error) {
console.log('Error parsing JSON: ', error.message);
}

if (!isValidJSON || data === null) {
return data;
if (!isValidJSON) {
return safeStringifyJSON(data);
}

if (filter) {
Expand All @@ -40,15 +44,15 @@ const formatResponse = (data, mode, filter) => {
}
}

return safeStringifyJSON(data, true);
return safeStringifyJSON(data);
}

if (mode.includes('xml')) {
let parsed = safeParseXML(data, { collapseContent: true });
if (typeof parsed === 'string') {
return parsed;
}
return safeStringifyJSON(parsed, true);
return safeStringifyJSON(parsed);
}

return data;
Expand Down

0 comments on commit 8175e3f

Please sign in to comment.