Skip to content

Commit c7baa45

Browse files
committed
Allow exit codes from 125 to 255
1 parent 92ac906 commit c7baa45

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ _Default_: `1`
9292

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

95-
Note: when passing invalid [`options`](#options), the exit code is always `125`.
95+
We recommend values between 1 and 124 because the following exit codes have some
96+
special meaning:
97+
98+
- 0: success
99+
- 125: invalid [`options`](#options)
100+
- 126 to 255: used by shells like Bash
96101

97102
#### 📕 stack
98103

src/exit.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ export const exitProcess = (exitCode, timeout) => {
2525
})
2626
}
2727

28-
// Minimum exit code
2928
const MIN_EXIT_CODE = 0
30-
// 126-255 have special meaning in Bash.
31-
const MAX_EXIT_CODE = 124
29+
export const MAX_EXIT_CODE = 255
3230
// 125 is reserved for invalid options with `handle-cli-error` itself.
3331
export const INVALID_OPTS_EXIT_CODE = 125
3432
// `options.exitCode` default value

src/exit.test.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import test from 'ava'
2+
import { each } from 'test-each'
23

34
// eslint-disable-next-line no-restricted-imports
4-
import { DEFAULT_EXIT_CODE } from './exit.js'
5+
import {
6+
DEFAULT_EXIT_CODE,
7+
INVALID_OPTS_EXIT_CODE,
8+
MAX_EXIT_CODE,
9+
} from './exit.js'
510
import { handleError } from './helpers/main.test.js'
611

712
test.serial('Default exit code', (t) => {
@@ -10,11 +15,15 @@ test.serial('Default exit code', (t) => {
1015
t.is(exitCodeAfter, DEFAULT_EXIT_CODE)
1116
})
1217

13-
test.serial('Custom exit code', (t) => {
14-
const customExitCode = 2
15-
const { exitCode, exitCodeAfter } = handleError('', {
16-
exitCode: customExitCode,
17-
})
18-
t.is(exitCode, customExitCode)
19-
t.is(exitCodeAfter, customExitCode)
20-
})
18+
each(
19+
[0, 2, INVALID_OPTS_EXIT_CODE, MAX_EXIT_CODE],
20+
({ title }, customExitCode) => {
21+
test.serial(`Custom exit code | ${title}`, (t) => {
22+
const { exitCode, exitCodeAfter } = handleError('', {
23+
exitCode: customExitCode,
24+
})
25+
t.is(exitCode, customExitCode)
26+
t.is(exitCodeAfter, customExitCode)
27+
})
28+
},
29+
)

src/main.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ export type Options = BeautifulErrorOptions & {
1212
/**
1313
* Process [exit code](https://en.wikipedia.org/wiki/Exit_status).
1414
*
15-
* Note: when passing invalid `options`, the exit code is always `125`.
15+
* We recommend values between 1 and 124 because the following exit codes have
16+
* some special meaning:
17+
*
18+
* - 0: success
19+
* - 125: invalid [`options`](#options)
20+
* - 126 to 255: used by shells like Bash
1621
*
1722
* @default 1
1823
*/

src/options/validate.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ each(
2222
{ exitCode: Number.POSITIVE_INFINITY },
2323
{ exitCode: Number.NEGATIVE_INFINITY },
2424
{ exitCode: -1 },
25-
{ exitCode: 125 },
26-
{ exitCode: 126 },
25+
{ exitCode: 256 },
2726
{ timeout: '0' },
2827
{ timeout: 0.1 },
2928
{ timeout: Number.NaN },

0 commit comments

Comments
 (0)