Skip to content

Commit

Permalink
refactor: Change factory export name to customErrorFactory
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the factory export name changed from `factory `to more expliit `customErrorFactory`
  • Loading branch information
adriengibrat committed Mar 14, 2018
1 parent e456e48 commit e8f51a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
18 changes: 9 additions & 9 deletions src/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface CustomConstructor<Properties> extends ErrorConstructor {
(...args): CustomError & Properties
}

export function factory<Properties = CustomError>(
export function customErrorFactory<Properties = CustomError>(
fn: (...Arguments) => void,
parent: ErrorConstructor = Error,
): CustomConstructor<Properties> {
Expand Down

0 comments on commit e8f51a0

Please sign in to comment.