Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: describe nothrow option #1109

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ The timeout option makes the process autokillable after the specified delay.
const p = $({timeout: '1s'})`sleep 999`
```

### `$({nothrow})`

The `nothrow` option suppresses errors and returns a `ProcessOutput` with details.

```js
const o1 = await $({nothrow: true})`exit 1`
o1.ok // false
o1.exitCode // 1
o1.message // exit code: 1 ...

const o2 = await $({nothrow: true, spawn() { throw new Error('BrokenSpawn') }})`echo foo`
o2.ok // false
o2.exitCode // null
o2.message // BrokenSpawn ...
```

The full options list:
```ts
interface Options {
Expand Down
2 changes: 1 addition & 1 deletion docs/process-promise.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ await $({stdio: ['pipe', 'pipe', 'pipe']})`read`

## `nothrow()`

Changes behavior of `$` to not throw an exception on non-zero exit codes.
Changes behavior of `$` to not throw an exception on non-zero exit codes. Equivalent to [`$({nothrow: true})` option](./api#nothrow).

```js
await $`grep something from-file`.nothrow()
Expand Down
18 changes: 18 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ describe('core', () => {
assert.equal($3`exit 3`.exitCode, 3)
})

test('handles `nothrow` option', async () => {
const o1 = await $({ nothrow: true })`exit 1`
assert.equal(o1.ok, false)
assert.equal(o1.exitCode, 1)
assert.match(o1.message, /exit code: 1/)

const o2 = await $({
nothrow: true,
spawn() {
throw new Error('BrokenSpawn')
},
})`echo foo`
assert.equal(o2.ok, false)
assert.equal(o2.exitCode, null)
assert.match(o2.message, /BrokenSpawn/)
})

test('handles `input` option', async () => {
const p1 = $({ input: 'foo' })`cat`
const p2 = $({ input: Readable.from('bar') })`cat`
Expand Down Expand Up @@ -1103,6 +1120,7 @@ describe('core', () => {
assert.equal(o.signal, 'SIGTERM')
assert.equal(o.exitCode, -1)
assert.equal(o.duration, 20)
assert.equal(o.ok, false)
assert.equal(Object.prototype.toString.call(o), '[object ProcessOutput]')
})

Expand Down