From d19f28e3f41b8f0248c11c49b7006754bb315218 Mon Sep 17 00:00:00 2001 From: Lars Mikkelsen Date: Wed, 8 Nov 2023 13:34:17 -0500 Subject: [PATCH] fix: use native ES2022 error cause (#5010) This improves error messages and stack traces. For example: Before: Error at new SubprocessError (.../dist/index.js:41:23) at ChildProcess. (.../dist/index.js:125:27) at ChildProcess.emit (node:events:513:28) at maybeClose (node:internal/child_process:1100:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:304:5) After: SubprocessError: Signal exit from subprocess. at ChildProcess. (.../dist/index.js:122:27) at ChildProcess.emit (node:events:513:28) at maybeClose (node:internal/child_process:1100:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:304:5) { code: 'ERR_SUBPROCESS_SIGNAL_EXIT', signal: 'SIGTERM' } --- packages/@ionic/cli/src/index.ts | 7 ++----- packages/@ionic/cli/src/lib/project/index.ts | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/@ionic/cli/src/index.ts b/packages/@ionic/cli/src/index.ts index fd9be4481..9b72945ba 100644 --- a/packages/@ionic/cli/src/index.ts +++ b/packages/@ionic/cli/src/index.ts @@ -3,6 +3,7 @@ import { readPackageJsonFile } from '@ionic/cli-framework/utils/node'; import { processExit } from '@ionic/utils-process'; import * as Debug from 'debug'; import * as path from 'path'; +import * as util from 'util'; import { IonicNamespace } from './commands'; import { IPCMessage, IonicContext, IonicEnvironment } from './definitions'; @@ -171,11 +172,7 @@ export async function run(pargv: string[]): Promise { } else if (err instanceof BaseError) { ienv.log.error(err.message); } else { - ienv.log.msg(failure(String(err.stack ? err.stack : err))); - - if (err.stack) { - debug(failure(String(err.stack))); - } + ienv.log.rawmsg(failure(util.inspect(err))); } } } diff --git a/packages/@ionic/cli/src/lib/project/index.ts b/packages/@ionic/cli/src/lib/project/index.ts index 6a1bfa7d6..77449ca6d 100644 --- a/packages/@ionic/cli/src/lib/project/index.ts +++ b/packages/@ionic/cli/src/lib/project/index.ts @@ -55,9 +55,9 @@ export class ProjectDetailsError extends BaseException { /** * The underlying error that caused this error. */ - readonly error?: Error + cause?: Error ) { - super(msg); + super(msg, { cause }); } } @@ -193,7 +193,7 @@ export class ProjectDetails { if (e1) { log.error( `Error while loading config (project config: ${strong(prettyPath(result.configPath))})\n` + - `${e1.error ? `${e1.message}: ${failure(e1.error.toString())}` : failure(e1.message)}. ` + + `${e1.cause ? `${e1.message}: ${failure(e1.cause.toString())}` : failure(e1.message)}. ` + `Run ${input('ionic init')} to re-initialize your Ionic project. Without a valid project config, the CLI will not have project context.` );