feat(cli): improve error handling with more custom Error instances

This commit is contained in:
Joakim Carlstein 2023-11-16 12:01:32 +01:00
parent 8dadfe9a5b
commit 30a448b4cf
6 changed files with 123 additions and 21 deletions

View file

@ -0,0 +1,71 @@
import { type MigrationHistoryEntry, type MigrationMetadata } from '@emigrate/plugin-tools/types';
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' });
export class EmigrateError extends Error {
constructor(
public code: string,
message: string,
options?: ErrorOptions,
) {
super(message, options);
}
}
export class ShowUsageError extends EmigrateError {}
export class MissingOptionError extends ShowUsageError {
constructor(public option: string | string[]) {
super('ERR_MISSING_OPT', `Missing required option: ${Array.isArray(option) ? formatter.format(option) : option}`);
}
}
export class MissingArgumentsError extends ShowUsageError {
constructor(public argument: string) {
super('ERR_MISSING_ARGS', `Missing required argument: ${argument}`);
}
}
export class BadOptionError extends ShowUsageError {
constructor(
public option: string,
message: string,
) {
super('ERR_BAD_OPT', message);
}
}
export class UnexpectedError extends EmigrateError {
constructor(message: string, options?: ErrorOptions) {
super('ERR_UNEXPECTED', message, options);
}
}
export class MigrationHistoryError extends EmigrateError {
constructor(
message: string,
public entry: MigrationHistoryEntry,
) {
super('ERR_MIGRATION_HISTORY', message);
}
}
export class MigrationLoadError extends EmigrateError {
constructor(
message: string,
public metadata: MigrationMetadata,
options?: ErrorOptions,
) {
super('ERR_MIGRATION_LOAD', message, options);
}
}
export class MigrationRunError extends EmigrateError {
constructor(
message: string,
public metadata: MigrationMetadata,
options?: ErrorOptions,
) {
super('ERR_MIGRATION_RUN', message, options);
}
}