Skip to content

Commit

Permalink
fix: use native ES2022 error cause (#5010)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `message`, `stack`, and `error` properties removed from `BaseError` and `SubprocessError`
  • Loading branch information
ltm committed Nov 8, 2023
1 parent b06ab3c commit a97ba2b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
17 changes: 1 addition & 16 deletions packages/@ionic/cli-framework/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as lodash from 'lodash';
import * as util from 'util';

import { ValidationError } from './definitions';
Expand All @@ -10,25 +9,11 @@ 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 {
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}` : ''}`
);
return util.inspect(this);
}

inspect(): string {
Expand Down
14 changes: 2 additions & 12 deletions packages/@ionic/utils-subprocess/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,11 @@ 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 @@ -172,13 +163,12 @@ export class Subprocess {
let err: SubprocessError;

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

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

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

0 comments on commit a97ba2b

Please sign in to comment.