Skip to content

Commit a86c54c

Browse files
eps1lonDavidRieman
andauthoredJul 22, 2024
feat: Reduce caught exceptions in prettyDom (reland) (#1323)
Co-authored-by: David Rieman <[email protected]> Co-authored-by: David Rieman <[email protected]>
1 parent 33555a3 commit a86c54c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
 

‎src/pretty-dom.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ import {getDocument} from './helpers'
55
import {getConfig} from './config'
66

77
const shouldHighlight = () => {
8+
if (typeof process === 'undefined') {
9+
// Don't colorize in non-node environments (e.g. Browsers)
10+
return false
11+
}
812
let colors
13+
// Try to safely parse env COLORS: We will default behavior if any step fails.
914
try {
10-
colors = JSON.parse(process?.env?.COLORS)
11-
} catch (e) {
12-
// If this throws, process?.env?.COLORS wasn't parsable. Since we only
15+
const colorsJSON = process.env?.COLORS
16+
if (colorsJSON) {
17+
colors = JSON.parse(colorsJSON)
18+
}
19+
} catch {
20+
// If this throws, process.env?.COLORS wasn't parsable. Since we only
1321
// care about `true` or `false`, we can safely ignore the error.
1422
}
1523

@@ -18,11 +26,7 @@ const shouldHighlight = () => {
1826
return colors
1927
} else {
2028
// If `colors` is not set, colorize if we're in node.
21-
return (
22-
typeof process !== 'undefined' &&
23-
process.versions !== undefined &&
24-
process.versions.node !== undefined
25-
)
29+
return process.versions !== undefined && process.versions.node !== undefined
2630
}
2731
}
2832

0 commit comments

Comments
 (0)