Skip to content

Commit

Permalink
chore: more tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jun 14, 2024
1 parent 2b705e3 commit d133466
Show file tree
Hide file tree
Showing 28 changed files with 814 additions and 529 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ module.exports = {

'@typescript-eslint/switch-exhaustiveness-check': 'error',

'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],

'@stylistic/ts/lines-around-comment': [
'warn',
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pluralize from 'pluralize';
import {AggregateSmokerError} from './base-error';
import {AggregateSmokerError} from './aggregate-smoker-error';
import {type UnsupportedPackageManagerError} from './unsupported-pkg-manager-error';

/**
Expand Down
61 changes: 61 additions & 0 deletions packages/midnight-smoker/src/error/aggregate-smoker-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {isZodError} from '#util/error-util';
import {castArray} from '#util/util';
import {fromZodError} from 'zod-validation-error';
import {BaseSmokerError, getErrorCode, type SmokerError} from './base-error';
import type {SmokerErrorCode, SmokerErrorId} from './codes';

/**
* Base class for all aggregate exceptions thrown by `midnight-smoker`.
*
* This should only be used if _multiple_ errors are being collected--not just
* catching some `Error` then throwing our own; use {@link BaseSmokerError} for
* that.
*
* @template Context - Arbitrary per-exception-class data to attach to the
* error.
* @group Errors
*/

export abstract class AggregateSmokerError<Context extends object | void = void>
extends AggregateError
implements SmokerError<Context>
{
public override readonly cause?: void;

public readonly context?: Context;

public readonly code: SmokerErrorCode;

public abstract readonly id: SmokerErrorId;

/**
* @privateRemarks
* Why doesn't the {@link AggregateError} constructor set this value?
*/
public override errors: Error[];

constructor(message: string, errors?: Error[] | Error, context?: Context) {
const errs = castArray(errors).map((err) =>
isZodError(err) ? fromZodError(err) : err,
);
super(errs, message);
this.context = context ?? undefined;
this.errors = errs;
this.code = getErrorCode(this);
}

public format(verbose = false) {
return BaseSmokerError.prototype.format.call(this, verbose);
}

public toJSON() {
return {
message: this.message,
id: this.id,
stack: this.stack,
context: this.context,
code: this.code,
errors: this.errors,
};
}
}
61 changes: 1 addition & 60 deletions packages/midnight-smoker/src/error/base-error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {isZodError} from '#util/error-util';
import {castArray} from '#util/util';
import {italic, white, whiteBright, yellow} from 'chalk';
import Debug from 'debug';
import {format, formatWithOptions} from 'node:util';
import stringify from 'stringify-object';
import {fromZodError} from 'zod-validation-error';
import type {SmokerErrorCode, SmokerErrorId} from './codes';
import {ErrorCodes} from './codes';

Expand All @@ -23,7 +20,7 @@ export const debug = Debug('midnight-smoker:error');
* Note that this will throw _during_ the instantiation of an `Error` about to
* be thrown.
*/
function getErrorCode(err: SmokerError<any, any>): SmokerErrorCode {
export function getErrorCode(err: SmokerError<any, any>): SmokerErrorCode {
const {name} = err.constructor;
if (!(name in ErrorCodes)) {
throw new ReferenceError(`${name} missing an error code`);
Expand Down Expand Up @@ -93,62 +90,6 @@ export abstract class BaseSmokerError<
}
}

/**
* Base class for all aggregate exceptions thrown by `midnight-smoker`.
*
* This should only be used if _multiple_ errors are being collected--not just
* catching some `Error` then throwing our own; use {@link BaseSmokerError} for
* that.
*
* @template Context - Arbitrary per-exception-class data to attach to the
* error.
* @group Errors
*/

export abstract class AggregateSmokerError<Context extends object | void = void>
extends AggregateError
implements SmokerError<Context>
{
public override readonly cause?: void;

public readonly context?: Context;

public readonly code: SmokerErrorCode;

public abstract readonly id: SmokerErrorId;

/**
* @privateRemarks
* Why doesn't the {@link AggregateError} constructor set this value?
*/
public override errors: Error[];

constructor(message: string, errors?: Error[] | Error, context?: Context) {
const errs = castArray(errors).map((err) =>
isZodError(err) ? fromZodError(err) : err,
);
super(errs, message);
this.context = context ?? undefined;
this.errors = errs;
this.code = getErrorCode(this);
}

public format(verbose = false) {
return BaseSmokerError.prototype.format.call(this, verbose);
}

public toJSON() {
return {
message: this.message,
id: this.id,
stack: this.stack,
context: this.context,
code: this.code,
errors: this.errors,
};
}
}

/**
* Options for {@link SmokerError} with a generic `Cause` type for `cause` prop.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/midnight-smoker/src/error/cleanup-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class CleanupError extends BaseSmokerError<
{
dir: string;
},
NodeJS.ErrnoException
Error
> {
public readonly id = 'CleanupError';

constructor(message: string, dir: string, error: NodeJS.ErrnoException) {
constructor(message: string, dir: string, error: Error) {
super(message, {dir}, error);
}
}
1 change: 1 addition & 0 deletions packages/midnight-smoker/src/error/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
export const ErrorCodes = {
AbortError: 'ESMOKER_ABORT',
AggregatePackError: 'ESMOKER_AGGREGATEPACKERROR',
AggregateUnsupportedPkgManagerError: 'ESMOKER_UNSUPPORTEDPACKAGEMANAGERS',
CleanupError: 'ESMOKER_CLEANUP',
ComponentCollisionError: 'ESMOKER_COMPONENTIDCOLLISION',
Expand Down
6 changes: 6 additions & 0 deletions packages/midnight-smoker/src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export * from './abort-error';

export * from './aggregate-pkg-manager-error';

export * from './aggregate-smoker-error';

export * from './base-error';

export * from './cleanup-error';
Expand Down Expand Up @@ -34,6 +36,8 @@ export * from './not-implemented-error';

export * from './pack-error';

export * from './pack-parse-error';

export * from './pkg-manager-error';

export * from './pkg-manager';
Expand Down Expand Up @@ -62,6 +66,8 @@ export * from './smoke-error';

export * from './smoker-reference-error';

export * from './some-pack-error';

export * from './temp-dir-error';

export * from './unknown-dist-tag-error';
Expand Down
2 changes: 1 addition & 1 deletion packages/midnight-smoker/src/error/machine-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {castArray} from 'lodash';
import {AggregateSmokerError} from './base-error';
import {AggregateSmokerError} from './aggregate-smoker-error';

/**
* Generic aggregate error for machines
Expand Down
25 changes: 0 additions & 25 deletions packages/midnight-smoker/src/error/pack-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,3 @@ export class PackError extends BaseSmokerError<
);
}
}

/**
* @group Errors
*/
export class PackParseError extends BaseSmokerError<
{
pkgManager: string;
output: string;
workspace: WorkspaceInfo;
},
SyntaxError
> {
public readonly id = 'PackParseError';

constructor(
message: string,
pkgManager: string | StaticPkgManagerSpec,
workspace: WorkspaceInfo,
error: SyntaxError,
output: string,
) {
const pmSpec = isString(pkgManager) ? pkgManager : pkgManager.spec;
super(message, {pkgManager: pmSpec, output, workspace}, error);
}
}
30 changes: 30 additions & 0 deletions packages/midnight-smoker/src/error/pack-parse-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {type StaticPkgManagerSpec} from '#schema/static-pkg-manager-spec';
import {type WorkspaceInfo} from '#schema/workspaces';
import {isString} from 'lodash';
import {BaseSmokerError} from './base-error';

/**
* @group Errors
*/

export class PackParseError extends BaseSmokerError<
{
pkgManager: string;
output: string;
workspace: WorkspaceInfo;
},
SyntaxError
> {
public readonly id = 'PackParseError';

constructor(
message: string,
pkgManager: string | StaticPkgManagerSpec,
workspace: WorkspaceInfo,
error: SyntaxError,
output: string,
) {
const pmSpec = isString(pkgManager) ? pkgManager : pkgManager.spec;
super(message, {pkgManager: pmSpec, output, workspace}, error);
}
}
6 changes: 5 additions & 1 deletion packages/midnight-smoker/src/error/pkg-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ export {InstallError} from '#error/install-error';

export {InvalidArgError} from '#error/invalid-arg-error';

export {PackError, PackParseError} from '#error/pack-error';
export {PackError} from '#error/pack-error';

export {PackParseError} from '#error/pack-parse-error';

export {RuleError} from '#error/rule-error';

export {RunScriptError} from '#error/run-script-error';

Expand Down
29 changes: 12 additions & 17 deletions packages/midnight-smoker/src/error/rule-error.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import type {StaticRuleContext} from '#schema/rule-static';
import {type LintManifest} from '#schema/lint-manifest';
import {type SomeRuleConfig} from '#schema/rule-options';
import {type Result} from '#schema/workspaces';
import {type Simplify} from 'type-fest';
import {BaseSmokerError} from './base-error';

export type RuleErrorContext = Simplify<
Result<LintManifest> & {
config: SomeRuleConfig;
ruleId: string;
}
>;

/**
* @group Errors
*/

export class RuleError extends BaseSmokerError<
{
context: StaticRuleContext;
ruleId: string;
},
Error
> {
export class RuleError extends BaseSmokerError<RuleErrorContext, Error> {
public readonly id = 'RuleError';

constructor(
message: string,
context: StaticRuleContext,
ruleId: string,
error: Error,
) {
super(message, {context, ruleId}, error);
}
}
2 changes: 1 addition & 1 deletion packages/midnight-smoker/src/error/smoke-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {type LintResult} from '#schema/lint-result';
import {type RunScriptResult} from '#schema/run-script-result';
import {castArray} from '#util/util';
import {AggregateSmokerError} from './base-error';
import {AggregateSmokerError} from './aggregate-smoker-error';

/**
* Thrown when _anything_ in `Smoker.smoke()` fails.
Expand Down
4 changes: 4 additions & 0 deletions packages/midnight-smoker/src/error/some-pack-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {type PackError} from './pack-error';
import {type PackParseError} from './pack-parse-error';

export type SomePackError = PackError | PackParseError;
7 changes: 2 additions & 5 deletions packages/midnight-smoker/src/error/temp-dir-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import {BaseSmokerError} from './base-error';
* @group Errors
*/

export class TempDirError extends BaseSmokerError<
{spec: string},
NodeJS.ErrnoException
> {
export class TempDirError extends BaseSmokerError<{spec: string}, Error> {
public readonly id = 'TempDirError';

constructor(
message: string,
spec: string | StaticPkgManagerSpec,
error: NodeJS.ErrnoException,
error: Error,
) {
super(message, {spec: isString(spec) ? spec : spec.spec}, error);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/midnight-smoker/src/event/pack-events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {type PackError, type PackParseError} from '#error/pack-error';
import {type PackError} from '#error/pack-error';
import {type SomePackError} from '#error/some-pack-error';
import {type InstallManifest} from '#schema/install-manifest';
import {type PackOptions} from '#schema/pack-options';
import {type StaticPkgManagerSpec} from '#schema/static-pkg-manager-spec';
import {type Result, type WorkspaceInfo} from '#schema/workspaces';
import {type PackParseError} from '../error/pack-parse-error';
import type {PackEvent} from './event-constants';
import type {PkgManagerEventBase} from './pkg-manager-events';

Expand Down Expand Up @@ -70,7 +72,7 @@ export interface PkgManagerPackBeginEventData

export interface PkgManagerPackFailedEventData
extends PkgManagerPackEventDataBase {
error: PackError | PackParseError;
error: SomePackError;
}

export interface PkgManagerPackOkEventData extends PkgManagerPackEventDataBase {
Expand All @@ -88,5 +90,5 @@ export interface PackBeginEventData extends PackEventDataBase {}
export interface PackOkEventData extends PackEventDataBase {}

export interface PackFailedEventData extends PackEventDataBase {
error: PackError | PackParseError;
error: SomePackError;
}
Loading

0 comments on commit d133466

Please sign in to comment.