Skip to content

Commit

Permalink
Add BaseError.exit()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Nov 13, 2022
1 parent e6ff34d commit 21e81c1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 2.2.0

## Features

- `error.exit()` has been renamed to
[`BaseError.exit(error)`](README.md#baseerrorexiterror). `error.exit()` is
deprecated but still supported.

# 2.1.0

## Features
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
[plugin](https://github.com/ehmicky/modern-errors#-plugins) to handle errors in
CLI modules.

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

# Features

Expand Down Expand Up @@ -44,17 +44,16 @@ export const BaseError = ModernError.subclass('BaseError', {
// ...
```

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

```js
const cliMain = function () {
try {
// ...
} catch (error) {
// Ensure `error` is a `BaseError` instance
const normalizedError = BaseError.normalize(error)
// Logs `error` then exits the process
normalizedError.exit()
BaseError.exit(error)
}
}

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

## error.exit()
## BaseError.exit(error)

`error`: `any`

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

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

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

```js
error.exit(...args, { ...options })
BaseError.exit(error, ...args, { ...options })
```

# Related projects
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ sinon.stub(process, 'exit')
// `handle-cli-error` use global variables `process.exitCode`, `process.exit()`
// and `console.error()` so we need to mock them.
// It also relies on timeout, which we need to mock as well.
export const errorExit = function (error, options) {
export const testErrorExit = function (BaseError, error, options) {
try {
// eslint-disable-next-line no-restricted-globals, no-console
console.error.resetHistory()
process.exit.resetHistory()

error.exit(options)
BaseError.exit(error, options)

// eslint-disable-next-line no-restricted-globals, no-console
const consoleArg = getStubArg(console.error)
Expand Down
7 changes: 4 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ModernError from 'modern-errors'
import modernErrorsCli from 'modern-errors-cli'
import { each } from 'test-each'

import { errorExit } from './helpers/main.js'
import { testErrorExit } from './helpers/main.js'

const exitCode = 5
const message = 'test'
Expand All @@ -13,12 +13,13 @@ const BaseError = ModernError.subclass('BaseError', {
cli: { timeout: 0 },
})
const error = new BaseError(message)
const errorExit = testErrorExit.bind(undefined, BaseError)

each(
[true, { timeout: 'true' }, { unknown: true }, { classes: {} }],
({ title }, cli) => {
({ title }, options) => {
test(`Options are validated | ${title}`, (t) => {
t.throws(error.exit.bind(error, cli))
t.throws(errorExit.bind(undefined, error, options))
})
},
)
Expand Down
4 changes: 1 addition & 3 deletions types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ declare const plugin: {
* try {
* // ...
* } catch (error) {
* // Ensure `error` is a `BaseError` instance
* const normalizedError = BaseError.normalize(error)
* // Logs `error` then exits the process
* normalizedError.exit()
* BaseError.exit(error)
* }
* }
*
Expand Down
14 changes: 7 additions & 7 deletions types/main.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ const BaseError = ModernError.subclass('BaseError', {
plugins: [modernErrorsCli],
})
const error = new BaseError('')
expectType<void>(error.exit())
expectType<void>(BaseError.exit(error))

ModernError.subclass('TestError', { plugins: [modernErrorsCli], cli: {} })
error.exit({})
BaseError.exit(error, {})
expectAssignable<Options>({})
expectError(error.exit(undefined))
expectError(BaseError.exit(error, undefined))
expectNotAssignable<Options>(undefined)
expectError(
ModernError.subclass('TestError', { plugins: [modernErrorsCli], cli: true }),
)
expectError(error.exit(true))
expectError(BaseError.exit(error, true))
expectNotAssignable<Options>(true)
expectError(
ModernError.subclass('TestError', {
plugins: [modernErrorsCli],
cli: { unknown: true },
}),
)
expectError(error.exit({ unknown: true }))
expectError(BaseError.exit(error, { unknown: true }))
expectNotAssignable<Options>({ unknown: true })

ModernError.subclass('TestError', {
plugins: [modernErrorsCli],
cli: { silent: true },
})
error.exit({ silent: true })
BaseError.exit(error, { silent: true })
expectAssignable<Options>({ silent: true })
expectError(
ModernError.subclass('TestError', {
plugins: [modernErrorsCli],
cli: { silent: 'true' },
}),
)
expectError(error.exit({ silent: 'true' }))
expectError(BaseError.exit(error, { silent: 'true' }))
expectNotAssignable<Options>({ silent: 'true' })

0 comments on commit 21e81c1

Please sign in to comment.