diff --git a/README.md b/README.md index 338d80c..8bf8d0c 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,14 @@ class HttpError extends CustomError { new HttpError(404, 'Not found') ``` -### Using the factory +More [advanced examples](src/example) + +### Using a factory + ```ts -import { factory } from 'ts-custom-error' +import { customErrorFactory } from 'ts-custom-error' -const HttpError = factory(function (code, message= '') { +const HttpError = customErrorFactory(function (code, message= '') { this.code = code this.message = message }) diff --git a/src/factory.spec.ts b/src/factory.spec.ts index 9a08c9a..520869d 100644 --- a/src/factory.spec.ts +++ b/src/factory.spec.ts @@ -1,22 +1,22 @@ import { checkProtoChain, checkProperties } from './spec.utils' -import { factory } from './factory' +import { customErrorFactory } from './factory' -const TestError = factory(function TestError() { +const TestError = customErrorFactory(function TestError() { /* noop */ }) test('Factory instance', () => checkProtoChain(TestError, Error)) test('Factory extended', () => { - const SubError = factory(function SubError() { + const SubError = customErrorFactory(function SubError() { /* noop */ }, TestError) checkProtoChain(SubError, TestError, Error) - checkProtoChain(factory(SubError, RangeError), RangeError, Error) + checkProtoChain(customErrorFactory(SubError, RangeError), RangeError, Error) }) test('Factory extended by class', () => { - const TestError = factory(function MyError() { + const TestError = customErrorFactory(function MyError() { /* noop */ }, RangeError) as ErrorConstructor class SubError extends TestError {} @@ -28,17 +28,17 @@ test('Factory properties', () => { this.code = code this.message = message } - checkProperties(factory(TestError), 'foo') + checkProperties(customErrorFactory(TestError), 'foo') function AnotherError(code = 2, message = 'bar') { this.code = code this.message = message } - checkProperties(factory(AnotherError), 'bar') + checkProperties(customErrorFactory(AnotherError), 'bar') - const ArgsError = factory<{ code: number }>( + const ArgsError = customErrorFactory<{ code: number }>( AnotherError, - factory(TestError), + customErrorFactory(TestError), ) const argsError = ArgsError(3, 'baz') expect(argsError.message).toBe('baz') diff --git a/src/factory.ts b/src/factory.ts index af92109..40bbbdc 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -12,7 +12,7 @@ interface CustomConstructor extends ErrorConstructor { (...args): CustomError & Properties } -export function factory( +export function customErrorFactory( fn: (...Arguments) => void, parent: ErrorConstructor = Error, ): CustomConstructor {