Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: use native ES2022 error cause #5060

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}
}