Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reduce caught exceptions in prettyDom (reland) #1323

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import {getDocument} from './helpers'
import {getConfig} from './config'

const shouldHighlight = () => {
if (typeof process === 'undefined') {
// Don't colorize in non-node environments (e.g. Browsers)
return false
}
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
const colorsJSON = process.env?.COLORS
if (colorsJSON) {
colors = JSON.parse(colorsJSON)
}
} catch {
// If this throws, process.env?.COLORS wasn't parsable. Since we only
// care about `true` or `false`, we can safely ignore the error.
}

Expand All @@ -18,11 +26,7 @@ const shouldHighlight = () => {
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
)
return process.versions !== undefined && process.versions.node !== undefined
}
}

Expand Down
Loading