Skip to content

Commit

Permalink
Fix disruptive unexceptional exceptions from being thrown in shouldHi…
Browse files Browse the repository at this point in the history
…ghlight 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.)
  • Loading branch information
DavidRieman committed Jun 25, 2024
1 parent afdff92 commit f7ccda2
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ import {getDocument} from './helpers'
import {getConfig} from './config'

const shouldHighlight = () => {
let colors
// Try to safely parse env COLORS: We will default behavior if any step fails.
try {
colors = JSON.parse(process?.env?.COLORS)
} catch (e) {
// If this throws, process?.env?.COLORS wasn't parsable. Since we only
// care about `true` or `false`, we can safely ignore the error.
const colors = process?.env?.COLORS
if (colors) {
const b = JSON.parse(colors)
if (typeof b === 'boolean') return b
}
} catch {
// Ignore (non-critical) - Make a defaulting choice below.
}

if (typeof colors === 'boolean') {
// If `colors` is set explicitly (both `true` and `false`), use that value.
return colors
} else {
// If `colors` is not set, colorize if we're in node.
return (
typeof process !== 'undefined' &&
process.versions !== undefined &&
process.versions.node !== undefined
)
}
// In all other cases, whether COLORS was a weird type, or the attempt threw:
// Fall back to colorizing if we are running in node.
return !!process?.versions?.node
}

const {DOMCollection} = prettyFormat.plugins
Expand Down

0 comments on commit f7ccda2

Please sign in to comment.