Skip to content

Commit e60e514

Browse files
committed
Change exitCode default value
1 parent a1e8708 commit e60e514

4 files changed

Lines changed: 22 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [`modern-errors@5`](https://github.com/ehmicky/modern-errors/releases/tag/5.0.0)
66
is now required
7+
- The default value for the [`exitCode` option](README.md#-exitcode) is now `1`
78

89
# 1.4.0
910

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ _Type_: `object`
9595

9696
### 🚨 exitCode
9797

98-
_Type_: `integer`
98+
_Type_: `integer`\
99+
_Default_: `1`
99100

100101
Process [exit code](https://en.wikipedia.org/wiki/Exit_status).
101102

102-
By default, each error class has its own exit code: `1` for the first one
103-
declared, `2` for the next one, and so on.
103+
Note: when passing invalid `options`, the exit code is always `125`.
104104

105105
### 📕 stack
106106

src/main.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,9 @@ const getOptions = function (options = {}) {
1010
return options
1111
}
1212

13-
// The default value of `exitCode` is 1 for the first declared class, then
14-
// incrementing from there.
15-
// - If some of the classes define their `exitCode`, it does not change the
16-
// default `exitCode` of others
1713
// Stack traces and error properties are displayed by default.
18-
const exit = function ({
19-
ErrorClasses,
20-
error,
21-
options: {
22-
stack = true,
23-
exitCode = Object.keys(ErrorClasses).indexOf(error.name) + 1,
24-
...options
25-
},
26-
}) {
27-
handleCliError(error, { ...options, stack, exitCode })
14+
const exit = function ({ error, options: { stack = true, ...options } }) {
15+
handleCliError(error, { ...options, stack })
2816
}
2917

3018
export default {

test/main.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import test from 'ava'
2-
import modernErrors from 'modern-errors'
2+
import ModernError from 'modern-errors'
33
import modernErrorsCli from 'modern-errors-cli'
44
import { each } from 'test-each'
55

66
import { errorExit } from './helpers/main.js'
77

8-
const globalOpts = { cli: { timeout: 0 } }
98
const exitCode = 5
109
const message = 'test'
1110

12-
const BaseError = modernErrors([modernErrorsCli], globalOpts)
13-
const UnknownError = BaseError.subclass('UnknownError')
14-
const OneError = BaseError.subclass('OneError')
15-
const TwoError = BaseError.subclass('TwoError', { cli: { exitCode } })
16-
const ThreeError = BaseError.subclass('ThreeError')
17-
18-
const error = new OneError(message)
11+
const BaseError = ModernError.subclass('BaseError', {
12+
plugins: [modernErrorsCli],
13+
cli: { timeout: 0 },
14+
})
15+
const error = new BaseError(message)
1916

2017
each(
2118
[true, { timeout: 'true' }, { unknown: true }, { classes: {} }],
@@ -30,16 +27,19 @@ test.serial('Call process.exit()', (t) => {
3027
t.true(Number.isInteger(errorExit(error).exitCode))
3128
})
3229

33-
test.serial('Can pass "exitCode"', (t) => {
30+
test.serial('Can pass "exitCode" as instance option', (t) => {
3431
t.is(errorExit(error, { exitCode }).exitCode, exitCode)
3532
})
3633

37-
test.serial('"exitCode" defaults to incrementing number', (t) => {
38-
t.is(errorExit(new UnknownError('')).exitCode, 1)
39-
t.is(errorExit(new OneError('')).exitCode, 2)
40-
t.is(errorExit(new TwoError('')).exitCode, exitCode)
41-
// eslint-disable-next-line no-magic-numbers
42-
t.is(errorExit(new ThreeError('')).exitCode, 4)
34+
test.serial('Can pass "exitCode" as class option', (t) => {
35+
const ExitCodeError = BaseError.subclass('ExitCodeError', {
36+
cli: { exitCode },
37+
})
38+
t.is(errorExit(new ExitCodeError('')).exitCode, exitCode)
39+
})
40+
41+
test.serial('"exitCode" defaults to 1', (t) => {
42+
t.is(errorExit(new BaseError('')).exitCode, 1)
4343
})
4444

4545
test.serial('Can pass "stack"', (t) => {

0 commit comments

Comments
 (0)