Skip to content

Commit 05c66d9

Browse files
DavidRiemaneps1lon
authored andcommitted
feat: Reduce caught exceptions in prettyDom (#1321)
Co-authored-by: David Rieman <[email protected]>
1 parent 67dee41 commit 05c66d9

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/pretty-dom.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ import {getDocument} from './helpers'
55
import {getConfig} from './config'
66

77
const shouldHighlight = () => {
8-
let colors
9-
try {
10-
colors = JSON.parse(process?.env?.COLORS)
11-
} catch (e) {
12-
// If this throws, process?.env?.COLORS wasn't parsable. Since we only
13-
// care about `true` or `false`, we can safely ignore the error.
8+
if (typeof process === 'undefined') {
9+
// Don't colorize in non-node environments (e.g. Browsers)
10+
return false
1411
}
15-
16-
if (typeof colors === 'boolean') {
17-
// If `colors` is set explicitly (both `true` and `false`), use that value.
18-
return colors
19-
} else {
12+
// Try to safely parse env COLORS: We will default behavior if any step fails.
13+
try {
14+
const colors = process.env?.COLORS
15+
if (colors) {
16+
const b = JSON.parse(colors)
17+
if (typeof b === 'boolean') {
18+
return b
19+
}
20+
}
21+
} catch {
2022
// 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-
)
23+
return process.versions !== undefined && process.versions.node !== undefined
2624
}
25+
26+
// In all other cases, whether COLORS was a weird type, or the attempt threw:
27+
// Fall back to colorizing if we are running in node.
28+
return !!process?.versions?.node
2729
}
2830

2931
const {DOMCollection} = prettyFormat.plugins

0 commit comments

Comments
 (0)