From 813fb3aba7c8510e6f61e64edc934129ad31dacd Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Tue, 8 Aug 2023 21:25:40 -0400 Subject: [PATCH] api: reduce console output when an error is thrown --- lib/shared/common.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/shared/common.ts b/lib/shared/common.ts index 5c200b04b16..608f79ff416 100644 --- a/lib/shared/common.ts +++ b/lib/shared/common.ts @@ -1653,7 +1653,7 @@ function parseStackLinesV8(streamIn: StreamIn, lines: string[], ident: string): function failureErrorWithLog(text: string, errors: types.Message[], warnings: types.Message[]): types.BuildFailure { let limit = 5 - let summary = errors.length < 1 ? '' : ` with ${errors.length} error${errors.length < 2 ? '' : 's'}:` + + text += errors.length < 1 ? '' : ` with ${errors.length} error${errors.length < 2 ? '' : 's'}:` + errors.slice(0, limit + 1).map((e, i) => { if (i === limit) return '\n...' if (!e.location) return `\nerror: ${e.text}` @@ -1661,9 +1661,26 @@ function failureErrorWithLog(text: string, errors: types.Message[], warnings: ty let pluginText = e.pluginName ? `[plugin: ${e.pluginName}] ` : '' return `\n${file}:${line}:${column}: ERROR: ${pluginText}${e.text}` }).join('') - let error: any = new Error(`${text}${summary}`) - error.errors = errors - error.warnings = warnings + let error: any = new Error(text) + + // Use a getter instead of a plain property so that when the error is thrown + // without being caught and the node process exits, the error objects aren't + // printed. The error objects are pretty big and not helpful because a) esbuild + // already prints errors to stderr by default and b) the error summary already + // has a more helpful abbreviated form of the error messages. + for (const [key, value] of [['errors', errors], ['warnings', warnings]] as const) { + Object.defineProperty(error, key, { + configurable: true, + enumerable: true, + get: () => value, + set: value => Object.defineProperty(error, key, { + configurable: true, + enumerable: true, + value, + }), + }) + } + return error }