Skip to content

Commit f7ccda2

Browse files
committed
Fix disruptive unexceptional exceptions from being thrown in shouldHighlight in pretty-dom. (These were often disruptive to developers who need to catch some other first-chance exception in the act as they may have to wade through these unexceptional occurrences on the way to get to their target first-chance exception.)
1 parent afdff92 commit f7ccda2

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/pretty-dom.js

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

77
const shouldHighlight = () => {
8-
let colors
8+
// Try to safely parse env COLORS: We will default behavior if any step fails.
99
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.
10+
const colors = process?.env?.COLORS
11+
if (colors) {
12+
const b = JSON.parse(colors)
13+
if (typeof b === 'boolean') return b
14+
}
15+
} catch {
16+
// Ignore (non-critical) - Make a defaulting choice below.
1417
}
1518

16-
if (typeof colors === 'boolean') {
17-
// If `colors` is set explicitly (both `true` and `false`), use that value.
18-
return colors
19-
} else {
20-
// 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-
)
26-
}
19+
// In all other cases, whether COLORS was a weird type, or the attempt threw:
20+
// Fall back to colorizing if we are running in node.
21+
return !!process?.versions?.node
2722
}
2823

2924
const {DOMCollection} = prettyFormat.plugins

0 commit comments

Comments
 (0)