Skip to content

feat(core): Error type factories #147

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

Merged
merged 1 commit into from
Apr 3, 2025
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
31 changes: 31 additions & 0 deletions packages/core/src/lib/helpers/TypeFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ArrayAssertion } from "../ArrayAssertion";
import { Assertion, Constructor } from "../Assertion";
import { BooleanAssertion } from "../BooleanAssertion";
import { DateAssertion } from "../DateAssertion";
import { ErrorAssertion } from "../ErrorAssertion";
import { type AnyFunction, FunctionAssertion } from "../FunctionAssertion";
import { NumberAssertion } from "../NumberAssertion";
import { ObjectAssertion } from "../ObjectAssertion";
Expand Down Expand Up @@ -47,6 +48,10 @@ export interface StaticTypeFactories {
* A `Date` TypeFactory.
*/
Date: TypeFactory<Date, DateAssertion>;
/**
* An `Error` TypeFactory.
*/
Error: TypeFactory<Error, ErrorAssertion<Error>>;
/**
* A `function` TypeFactory.
*/
Expand All @@ -71,6 +76,20 @@ export interface StaticTypeFactories {
* @param innerType the TypeFactory for the array type
*/
array<T>(innerType?: TypeFactory<T, Assertion<T>>): TypeFactory<T[], ArrayAssertion<T>>;
/**
* Creates an `Error` TypeFactory for a specific error constructor.
*
* @example
* ```
* class CustomError extends Error { ... }
*
* TypeFactories.error(CustomError); // a `CustomError` error factory
* ```
*
* @typeParam T the type of the error constructor
* @param Type the error constructor
*/
error<T extends Error>(Type: Constructor<T>): TypeFactory<T, ErrorAssertion<T>>;
/**
* Creates a TypeFactory for an instance of the given constructor.
*
Expand Down Expand Up @@ -114,6 +133,11 @@ export const TypeFactories: Readonly<StaticTypeFactories> = {
predicate: (value): value is Date => value instanceof Date,
typeName: Date.name,
},
Error: {
Factory: ErrorAssertion,
predicate: (value): value is Error => value instanceof Error,
typeName: Error.name,
},
Function: {
Factory: FunctionAssertion,
predicate: (value): value is AnyFunction => typeof value === "function",
Expand All @@ -139,6 +163,13 @@ export const TypeFactories: Readonly<StaticTypeFactories> = {
typeName: "array",
};
},
error<T extends Error>(Type: Constructor<T>) {
return {
Factory: ErrorAssertion,
predicate: (value): value is T => value instanceof Type,
typeName: Type.name,
};
},
instanceOf<T>(type: Constructor<T>) {
return {
Factory: Assertion,
Expand Down
47 changes: 47 additions & 0 deletions packages/core/test/lib/helpers/TypeFactories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { TypeFactories } from "../../../src/lib/helpers/TypeFactories";

import assert from "assert";

class CustomError extends Error {

public constructor(message?: string) {
super(message);

this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
Object.setPrototypeOf(this, CustomError.prototype);
}
}

describe("[Unit] TypeFactories.test.ts", () => {
describe(".predicate", () => {
describe("#Boolean", () => {
Expand Down Expand Up @@ -42,6 +53,24 @@ describe("[Unit] TypeFactories.test.ts", () => {
});
});

describe("#Error", () => {
context("when the value is an Error", () => {
it("returns true", () => {
const result = TypeFactories.Error.predicate(new Error("foo"));

assert.equal(result, true);
});
});

context("when the value is not an Error", () => {
it("returns false", () => {
const result = TypeFactories.Error.predicate("foo");

assert.equal(result, false);
});
});
});

describe("#Function", () => {
context("when the value is a function", () => {
it("returns true", () => {
Expand Down Expand Up @@ -143,6 +172,24 @@ describe("[Unit] TypeFactories.test.ts", () => {
});
});

describe(".error", () => {
context("when the value is an instace o the error type", () => {
it("returns true", () => {
const result = TypeFactories.error(CustomError).predicate(new CustomError("foo"));

assert.equal(result, true);
});
});

context("when the value is not an instace o the error type", () => {
it("returns false", () => {
const result = TypeFactories.error(CustomError).predicate(new Error("foo"));

assert.equal(result, false);
});
});
});

describe(".instanceOf", () => {
context("when the value is an instance of the passed type", () => {
it("returns true", () => {
Expand Down