fix: handle case of text when invalid JSON (#3119) (#3120)

* fix: handle case of text when invalid JSON (#3119)

* don't stringify if json is invalid, and maintain indentation if stringified

* stringify check

---------

Co-authored-by: lohit <lohxt.space@gmail.com>
This commit is contained in:
Théo D 2024-09-18 11:54:33 +02:00 committed by GitHub
parent 8b6e55dfc0
commit 938e0560a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,16 +19,20 @@ 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) {
if (!isValidJSON && typeof data === 'string') {
return data;
}