From a33e08e81cea544e1f9b6b3e915862d7d02ad8aa Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 28 Apr 2020 19:15:36 +0200 Subject: [PATCH] errors: skip fatal error highlighting on windows Some consoles do not convert ANSI escape sequences to colors, rather display them directly to the stdout. On those consoles, libuv emulates colors by intercepting stdout stream and calling corresponding Windows API functions for setting console colors. However, fatal error are handled differently and we cannot easily highlight them. PR-URL: https://github.com/nodejs/node/pull/33132 Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater --- lib/internal/errors.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index f20577f8bc66af..6640f7597c8128 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -674,15 +674,31 @@ const fatalExceptionStackEnhancers = { }, afterInspector(error) { const originalStack = error.stack; + let useColors = true; + // Some consoles do not convert ANSI escape sequences to colors, + // rather display them directly to the stdout. On those consoles, + // libuv emulates colors by intercepting stdout stream and calling + // corresponding Windows API functions for setting console colors. + // However, fatal error are handled differently and we cannot easily + // highlight them. On Windows, detecting whether a console supports + // ANSI escape sequences is not reliable. + if (process.platform === 'win32') { + const info = internalBinding('os').getOSInformation(); + const ver = info[2].split('.').map((a) => +a); + if (ver[0] !== 10 || ver[2] < 14393) { + useColors = false; + } + } const { inspect, inspectDefaultOptions: { colors: defaultColors } } = lazyInternalUtilInspect(); - const colors = (internalBinding('util').guessHandleType(2) === 'TTY' && + const colors = useColors && + ((internalBinding('util').guessHandleType(2) === 'TTY' && require('internal/tty').hasColors()) || - defaultColors; + defaultColors); try { return inspect(error, { colors }); } catch {