Skip to content

Commit bd67961

Browse files
authored
Improve pretty print handling of string
If the API response is a JSON string (eg. it only returns an IP as a string like `"192.168.1.1"`) then this will now be able to gracefully handle that instead of dying from an uncaught JSON parse exception.
1 parent 0802e2b commit bd67961

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

utils/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const path = require('path');
44
module.exports = {
55
prettyPrint: function(output) {
66
if (typeof output === 'string' || output instanceof String) {
7-
output = JSON.parse(output);
7+
try {
8+
output = JSON.parse(output);
9+
}
10+
catch (err) {
11+
// Do nothing; it may have already been parsed (if the JSON was a string and not an object)
12+
}
813
}
914
return JSON.stringify(output, null, 2);
1015
},

0 commit comments

Comments
 (0)