Skip to content

Commit 21e81c1

Browse files
committed
Add BaseError.exit()
1 parent e6ff34d commit 21e81c1

File tree

6 files changed

+32
-24
lines changed

6 files changed

+32
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 2.2.0
2+
3+
## Features
4+
5+
- `error.exit()` has been renamed to
6+
[`BaseError.exit(error)`](README.md#baseerrorexiterror). `error.exit()` is
7+
deprecated but still supported.
8+
19
# 2.1.0
210

311
## Features

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
[plugin](https://github.com/ehmicky/modern-errors#-plugins) to handle errors in
1414
CLI modules.
1515

16-
This adds [`error.exit()`](#errorexit) which logs `error` then exits the
17-
process.
16+
This adds [`BaseError.exit(error)`](#baseerrorexiterror) which logs `error` then
17+
exits the process.
1818

1919
# Features
2020

@@ -44,17 +44,16 @@ export const BaseError = ModernError.subclass('BaseError', {
4444
// ...
4545
```
4646

47-
Calling [`error.exit()`](#errorexit) in the CLI's top-level error handler.
47+
Calling [`BaseError.exit(error)`](#baseerrorexiterror) in the CLI's top-level
48+
error handler.
4849

4950
```js
5051
const cliMain = function () {
5152
try {
5253
// ...
5354
} catch (error) {
54-
// Ensure `error` is a `BaseError` instance
55-
const normalizedError = BaseError.normalize(error)
5655
// Logs `error` then exits the process
57-
normalizedError.exit()
56+
BaseError.exit(error)
5857
}
5958
}
6059

@@ -82,7 +81,9 @@ Plugin object to pass to the
8281
[`plugins` option](https://github.com/ehmicky/modern-errors#adding-plugins) of
8382
`ErrorClass.subclass()`.
8483

85-
## error.exit()
84+
## BaseError.exit(error)
85+
86+
`error`: `any`
8687

8788
Logs `error` on the console (`stderr`) then exits the process.
8889

@@ -196,10 +197,10 @@ export const InputError = BaseError.subclass('InputError', {
196197
throw new InputError('...', { cli: { ...options } })
197198
```
198199

199-
- A specific [`error.exit()`](#errorexit) call
200+
- A specific [`BaseError.exit(error)`](#baseerrorexiterror) call
200201

201202
```js
202-
error.exit(...args, { ...options })
203+
BaseError.exit(error, ...args, { ...options })
203204
```
204205

205206
# Related projects

test/helpers/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ sinon.stub(process, 'exit')
99
// `handle-cli-error` use global variables `process.exitCode`, `process.exit()`
1010
// and `console.error()` so we need to mock them.
1111
// It also relies on timeout, which we need to mock as well.
12-
export const errorExit = function (error, options) {
12+
export const testErrorExit = function (BaseError, error, options) {
1313
try {
1414
// eslint-disable-next-line no-restricted-globals, no-console
1515
console.error.resetHistory()
1616
process.exit.resetHistory()
1717

18-
error.exit(options)
18+
BaseError.exit(error, options)
1919

2020
// eslint-disable-next-line no-restricted-globals, no-console
2121
const consoleArg = getStubArg(console.error)

test/main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ModernError from 'modern-errors'
33
import modernErrorsCli from 'modern-errors-cli'
44
import { each } from 'test-each'
55

6-
import { errorExit } from './helpers/main.js'
6+
import { testErrorExit } from './helpers/main.js'
77

88
const exitCode = 5
99
const message = 'test'
@@ -13,12 +13,13 @@ const BaseError = ModernError.subclass('BaseError', {
1313
cli: { timeout: 0 },
1414
})
1515
const error = new BaseError(message)
16+
const errorExit = testErrorExit.bind(undefined, BaseError)
1617

1718
each(
1819
[true, { timeout: 'true' }, { unknown: true }, { classes: {} }],
19-
({ title }, cli) => {
20+
({ title }, options) => {
2021
test(`Options are validated | ${title}`, (t) => {
21-
t.throws(error.exit.bind(error, cli))
22+
t.throws(errorExit.bind(undefined, error, options))
2223
})
2324
},
2425
)

types/main.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ declare const plugin: {
2626
* try {
2727
* // ...
2828
* } catch (error) {
29-
* // Ensure `error` is a `BaseError` instance
30-
* const normalizedError = BaseError.normalize(error)
3129
* // Logs `error` then exits the process
32-
* normalizedError.exit()
30+
* BaseError.exit(error)
3331
* }
3432
* }
3533
*

types/main.test-d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ const BaseError = ModernError.subclass('BaseError', {
1111
plugins: [modernErrorsCli],
1212
})
1313
const error = new BaseError('')
14-
expectType<void>(error.exit())
14+
expectType<void>(BaseError.exit(error))
1515

1616
ModernError.subclass('TestError', { plugins: [modernErrorsCli], cli: {} })
17-
error.exit({})
17+
BaseError.exit(error, {})
1818
expectAssignable<Options>({})
19-
expectError(error.exit(undefined))
19+
expectError(BaseError.exit(error, undefined))
2020
expectNotAssignable<Options>(undefined)
2121
expectError(
2222
ModernError.subclass('TestError', { plugins: [modernErrorsCli], cli: true }),
2323
)
24-
expectError(error.exit(true))
24+
expectError(BaseError.exit(error, true))
2525
expectNotAssignable<Options>(true)
2626
expectError(
2727
ModernError.subclass('TestError', {
2828
plugins: [modernErrorsCli],
2929
cli: { unknown: true },
3030
}),
3131
)
32-
expectError(error.exit({ unknown: true }))
32+
expectError(BaseError.exit(error, { unknown: true }))
3333
expectNotAssignable<Options>({ unknown: true })
3434

3535
ModernError.subclass('TestError', {
3636
plugins: [modernErrorsCli],
3737
cli: { silent: true },
3838
})
39-
error.exit({ silent: true })
39+
BaseError.exit(error, { silent: true })
4040
expectAssignable<Options>({ silent: true })
4141
expectError(
4242
ModernError.subclass('TestError', {
4343
plugins: [modernErrorsCli],
4444
cli: { silent: 'true' },
4545
}),
4646
)
47-
expectError(error.exit({ silent: 'true' }))
47+
expectError(BaseError.exit(error, { silent: 'true' }))
4848
expectNotAssignable<Options>({ silent: 'true' })

0 commit comments

Comments
 (0)