Skip to content

Commit

Permalink
revert: use native ES2022 error cause (#5060)
Browse files Browse the repository at this point in the history
This reverts commit 0c4cd0f.
  • Loading branch information
ltm authored Nov 8, 2023
1 parent b0af2be commit 1e64a1a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
17 changes: 16 additions & 1 deletion packages/@ionic/cli-framework/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as lodash from 'lodash';
import * as util from 'util';

import { ValidationError } from './definitions';
Expand All @@ -9,11 +10,25 @@ export const ERROR_IPC_UNKNOWN_PROCEDURE = 'ERR_ICF_IPC_UNKNOWN_PROCEDURE';

export abstract class BaseError extends Error {
abstract readonly name: string;
message: string;
stack: string;
code?: string;
error?: Error;
exitCode?: number;

constructor(message: string) {
super(message);
this.message = message;
this.stack = (new Error()).stack || '';
}

toString(): string {
return util.inspect(this);
const repr = lodash.pick(this, lodash.pull(lodash.keys(this), 'error'));

return (
`${this.name}: ${this.message} ${util.inspect(repr, { breakLength: Infinity })} ${this.stack} ` +
`${this.error ? `\nWrapped: ${this.error.stack ? this.error.stack : this.error}` : ''}`
);
}

inspect(): string {
Expand Down
7 changes: 5 additions & 2 deletions packages/@ionic/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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';
Expand Down Expand Up @@ -172,7 +171,11 @@ export async function run(pargv: string[]): Promise<void> {
} else if (err instanceof BaseError) {
ienv.log.error(err.message);
} else {
ienv.log.rawmsg(failure(util.inspect(err)));
ienv.log.msg(failure(String(err.stack ? err.stack : err)));

if (err.stack) {
debug(failure(String(err.stack)));
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@ionic/cli/src/lib/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export class ProjectDetailsError extends BaseException {
/**
* The underlying error that caused this error.
*/
cause?: Error
readonly error?: Error
) {
super(msg, { cause });
super(msg);
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ export class ProjectDetails {
if (e1) {
log.error(
`Error while loading config (project config: ${strong(prettyPath(result.configPath))})\n` +
`${e1.cause ? `${e1.message}: ${failure(e1.cause.toString())}` : failure(e1.message)}. ` +
`${e1.error ? `${e1.message}: ${failure(e1.error.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.`
);

Expand Down
14 changes: 12 additions & 2 deletions packages/@ionic/utils-subprocess/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ export function convertPATH(path = process.env.PATH || ''): string {

export class SubprocessError extends Error {
readonly name = 'SubprocessError';
message: string;
stack: string;

code?: typeof ERROR_COMMAND_NOT_FOUND | typeof ERROR_NON_ZERO_EXIT | typeof ERROR_SIGNAL_EXIT;
error?: Error;
output?: string;
signal?: string;
exitCode?: number;

constructor(message: string) {
super(message);
this.message = message;
this.stack = (new Error()).stack || '';
}
}

export interface SubprocessOptions extends SpawnOptions {}
Expand Down Expand Up @@ -163,12 +172,13 @@ export class Subprocess {
let err: SubprocessError;

if (error.code === 'ENOENT') {
err = new SubprocessError('Command not found.', { cause: error });
err = new SubprocessError('Command not found.');
err.code = ERROR_COMMAND_NOT_FOUND;
} else {
err = new SubprocessError('Command error.', { cause: error });
err = new SubprocessError('Command error.');
}

err.error = error;
reject(err);
});

Expand Down
3 changes: 1 addition & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"target": "ES2021",
"types": [],
"lib": [
"ES2021",
"ES2022.Error"
"ES2021"
]
}
}

0 comments on commit 1e64a1a

Please sign in to comment.