From 994d500b5af7d809bbcedc032697fd2415e794ea Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 30 May 2017 06:17:23 -0700 Subject: [PATCH] packager: Server: more consistent error handling Reviewed By: cpojer Differential Revision: D5137887 fbshipit-source-id: fe73f9a74ac7091fcad66d91859e2ff90733ce45 --- packager/src/Server/index.js | 10 ++++++---- packager/src/lib/TerminalReporter.js | 18 +++++++++++++++--- packager/src/lib/reporting.js | 4 +++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packager/src/Server/index.js b/packager/src/Server/index.js index 88b7bfcc3..9c394d926 100644 --- a/packager/src/Server/index.js +++ b/packager/src/Server/index.js @@ -507,7 +507,6 @@ class Server { }, error => { this._reporter.update({ entryFilePath: options.entryFile, - error, type: 'bundle_build_failed', }); return Promise.reject(error); @@ -800,9 +799,11 @@ class Server { 'Content-Type': 'application/json; charset=UTF-8', }); - if (error.type === 'TransformError' || - error.type === 'NotFoundError' || - error.type === 'UnableToResolveError') { + if (error instanceof Error && ( + error.type === 'TransformError' || + error.type === 'NotFoundError' || + error.type === 'UnableToResolveError' + )) { error.errors = [{ description: error.description, filename: error.filename, @@ -813,6 +814,7 @@ class Server { if (error.type === 'NotFoundError') { delete this._bundles[bundleID]; } + this._reporter.update({error, type: 'bundling_error'}); } else { console.error(error.stack || error); res.end(JSON.stringify({ diff --git a/packager/src/lib/TerminalReporter.js b/packager/src/lib/TerminalReporter.js index 41e6c46f4..2632fcfc8 100644 --- a/packager/src/lib/TerminalReporter.js +++ b/packager/src/lib/TerminalReporter.js @@ -126,8 +126,7 @@ class TerminalReporter { } } - _logBundleBuildFailed(entryFilePath: string, error: Error) { - reporting.logError(terminal, 'bundling: %s', error.stack); + _logBundleBuildFailed(entryFilePath: string) { const progress = this._activeBundles.get(entryFilePath); if (progress != null) { const msg = this._getBundleStatusMessage(entryFilePath, progress, 'failed'); @@ -201,7 +200,10 @@ class TerminalReporter { this._logBundleBuildDone(event.entryFilePath); break; case 'bundle_build_failed': - this._logBundleBuildFailed(event.entryFilePath, event.error); + this._logBundleBuildFailed(event.entryFilePath); + break; + case 'bundling_error': + this._logBundlingError(event.error); break; case 'dep_graph_loaded': terminal.log(`${DEP_GRAPH_MESSAGE}, done.`); @@ -221,6 +223,16 @@ class TerminalReporter { } } + /** + * We do not want to log the whole stacktrace for bundling error, because + * these are operational errors, not programming errors, and the stacktrace + * is not actionable to end users. + */ + _logBundlingError(error: Error) { + const str = JSON.stringify(error.message); + reporting.logError(terminal, 'bundling failed: %s', str); + } + _logWorkerChunk(origin: 'stdout' | 'stderr', chunk: string) { const lines = chunk.split('\n'); if (lines.length >= 1 && lines[lines.length - 1] === '') { diff --git a/packager/src/lib/reporting.js b/packager/src/lib/reporting.js index fdc84acb0..f2005bb31 100644 --- a/packager/src/lib/reporting.js +++ b/packager/src/lib/reporting.js @@ -37,11 +37,13 @@ export type ReportableEvent = { type: 'bundle_build_done', } | { entryFilePath: string, - error: Error, type: 'bundle_build_failed', } | { entryFilePath: string, type: 'bundle_build_started', +} | { + error: Error, + type: 'bundling_error', } | { type: 'dep_graph_loading', } | {