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

refactor(errors 🔧): no reason to delay creating error message #316

Merged
merged 1 commit into from
Oct 11, 2020
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
2 changes: 1 addition & 1 deletion goldens/api/@betterer/cli.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export declare type BettererCLIArguments = ReadonlyArray<string>;
export declare type BettererCLIArguments = Array<string>;

export declare type BettererCLICIConfig = {
config: BettererCLIArguments;
Expand Down
6 changes: 4 additions & 2 deletions goldens/api/@betterer/errors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ export declare type BettererError = Error & {
details: BettererErrorDetails;
};

export declare type BettererErrorDetails = ReadonlyArray<string | Error | BettererError>;
export declare type BettererErrorDetail = string | ErrorLike | BettererError;

export declare type BettererErrorDetails = ReadonlyArray<BettererErrorDetail>;

export declare type BettererErrorFactory = (...details: BettererErrorDetails) => BettererError;

Expand All @@ -16,4 +18,4 @@ export declare type ErrorLike = {

export declare function logErrorΔ(err: ErrorLike | Error | BettererError): void;

export declare function registerError(factory: BettererErrorMessageFactory): BettererErrorFactory;
export declare function registerError(messageFactory: BettererErrorMessageFactory): BettererErrorFactory;
19 changes: 9 additions & 10 deletions packages/errors/src/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import { brΔ, errorΔ } from '@betterer/logger';
import { BettererErrorΩ } from './error';
import {
BettererError,
BettererErrorDetail,
BettererErrorDetails,
BettererErrorFactory,
BettererErrorMessageFactory,
ErrorLike
} from './types';

const ERROR_MESSAGES = new Map<symbol, BettererErrorMessageFactory>();
const ERROR_CODES: Array<symbol> = [];

export function logErrorΔ(err: ErrorLike | Error | BettererError): void {
if (isBettererError(err)) {
const factory = ERROR_MESSAGES.get(err.code) as BettererErrorMessageFactory;
const errors = err.details.filter((detail) => isErrorLike(detail)) as Array<ErrorLike>;
const messages = err.details.filter((detail) => !errors.includes(detail as ErrorLike)) as Array<string>;
err.message = factory(...messages);
errorΔ(err.message);
errors.forEach(logErrorΔ);
return;
Expand All @@ -27,20 +25,21 @@ export function logErrorΔ(err: ErrorLike | Error | BettererError): void {
brΔ();
}

export function registerError(factory: BettererErrorMessageFactory): BettererErrorFactory {
export function registerError(messageFactory: BettererErrorMessageFactory): BettererErrorFactory {
const code = Symbol();
ERROR_MESSAGES.set(code, factory);
ERROR_CODES.push(code);
return function factory(...details: BettererErrorDetails): BettererError {
const error = new BettererErrorΩ(code, ...details);
const messages = details.filter((detail) => !isErrorLike(detail)) as Array<string>;
const error = new BettererErrorΩ(messageFactory(...messages), code, ...details);
Error.captureStackTrace(error, factory);
return error;
};
}

function isBettererError(err: unknown): err is BettererError {
return !!ERROR_MESSAGES.has((err as BettererError).code);
function isBettererError(err: ErrorLike | Error | BettererError): err is BettererError {
return !!ERROR_CODES.includes((err as BettererError).code);
}

function isErrorLike(err: unknown): err is ErrorLike {
function isErrorLike(err: BettererErrorDetail): err is ErrorLike {
return (err as ErrorLike).message != null && (err as ErrorLike).stack !== null;
}
4 changes: 2 additions & 2 deletions packages/errors/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { BettererError, BettererErrorDetails } from './types';
export class BettererErrorΩ extends Error implements BettererError {
public details: BettererErrorDetails;

constructor(public code: symbol, ...details: BettererErrorDetails) {
super();
constructor(message: string, public code: symbol, ...details: BettererErrorDetails) {
super(message);

Error.captureStackTrace(this, BettererErrorΩ);
Object.setPrototypeOf(this, new.target.prototype);
Expand Down
1 change: 1 addition & 0 deletions packages/errors/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { logErrorΔ, registerError } from './error-handler';
export {
BettererError,
BettererErrorDetail,
BettererErrorDetails,
BettererErrorFactory,
BettererErrorMessageFactory,
Expand Down
3 changes: 2 additions & 1 deletion packages/errors/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export type BettererError = Error & {
details: BettererErrorDetails;
};

export type BettererErrorDetails = ReadonlyArray<string | Error | BettererError>;
export type BettererErrorDetail = string | ErrorLike | BettererError;
export type BettererErrorDetails = ReadonlyArray<BettererErrorDetail>;
export type BettererErrorFactory = (...details: BettererErrorDetails) => BettererError;
export type BettererErrorMessageFactory = (...details: BettererErrorDetails) => string;

Expand Down