Commit b571839

Anton Golub <antongolub@antongolub.com>
2025-02-23 15:02:17
docs: describe `nothrow` option (#1109)
1 parent 1ef794a
docs/api.md
@@ -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 {
docs/process-promise.md
@@ -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()
test/core.test.js
@@ -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`
@@ -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]')
     })