Skip to content
Myr

errors.md

This file

Path

docs/errors.md

110 lines · 4.5 KiB · Markdown

Last changed by

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–10why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# ❌ Errors89## Subprocess failure10

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 11why, one step back
11When the subprocess fails, the promise returned by [`execa()`](api.md#execafile-arguments-options) is rejected with an [`ExecaError`](api.md#execaerror) instance. The `error` has the same shape as successful [results](api.md#result), with a few additional [error-specific fields](api.md#execaerror). [`error.failed`](api.md#resultfailed) is always `true`.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 12–27why, one step back
12
13```js14import {execa, ExecaError} from 'execa';1516try {17 const result = await execa`npm run build`;18 console.log(result.failed); // false19} catch (error) {20 if (error instanceof ExecaError) {21 console.error(error.failed); // true22 }23}24```2526## Preventing exceptions27

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 28why, one step back
28When the [`reject`](api.md#optionsreject) option is `false`, the `error` is returned instead.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 29–30why, one step back
29
30```js

Fix "Preventing exceptions" example (#1110)Karl Horkybefore adoptiona5a4d69

line 31why, one step back
31const resultOrError = await execa({reject: false})`npm run build`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 32–38why, one step back
32if (resultOrError.failed) {
33 console.error(resultOrError);34}35```3637## Exit code38

Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec

line 39why, one step back
39The subprocess fails when its [exit code](https://en.wikipedia.org/wiki/Exit_status) is not `0`. The exit code is available as [`error.exitCode`](api.md#errorexitcode). It is `undefined` when the subprocess fails to spawn or when it was [terminated by a signal](termination.md#signal-termination).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 40–52why, one step back
40
41```js42try {43 await execa`npm run build`;44} catch (error) {45 // Either non-0 integer or undefined46 console.error(error.exitCode);47}48```4950## Failure reason5152The subprocess can fail for other reasons. Some of them can be detected using a specific boolean property:

Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec

lines 53–54why, one step back
53- [`error.timedOut`](api.md#errortimedout): [`timeout`](termination.md#timeout) option.
54- [`error.isCanceled`](api.md#erroriscanceled): [`cancelSignal`](termination.md#canceling) option.

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 55why, one step back
55- [`error.isGracefullyCanceled`](api.md#errorisgracefullycanceled): `cancelSignal` option, if the [`gracefulCancel`](termination.md#graceful-termination) option is `true`.

Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec

line 56why, one step back
56- [`error.isMaxBuffer`](api.md#errorismaxbuffer): [`maxBuffer`](output.md#big-output) option.

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 57why, one step back
57- [`error.isTerminated`](api.md#erroristerminated): [signal termination](termination.md#signal-termination). This includes the [`timeout`](termination.md#timeout) and [`forceKillAfterDelay`](termination.md#forceful-termination) options since those terminate the subprocess with a [signal](termination.md#default-signal). This also includes the [`cancelSignal`](termination.md#canceling) option unless the [`gracefulCancel`](termination.md#graceful-termination) option is `true`. This does not include the [`maxBuffer`](output.md#big-output) option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 58–61why, one step back
58
59Otherwise, the subprocess failed because either:60- An exception was thrown in a [stream](streams.md) or [transform](transform.md).61- The command's executable file was not found.

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 62why, one step back
62- An invalid [option](api.md#options-1) was passed.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 63–78why, one step back
63- There was not enough memory or too many subprocesses.
6465```js66try {67 await execa`npm run build`;68} catch (error) {69 if (error.timedOut) {70 handleTimeout(error);71 }7273 throw error;74}75```7677## Error message78

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 79why, one step back
79For better [debugging](debugging.md), [`error.message`](api.md#errormessage) includes both:

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 80why, one step back
80- The command and the [reason it failed](#failure-reason).

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 81why, one step back
81- Its [`stdout`, `stderr`](output.md#stdout-and-stderr), [other file descriptors'](output.md#additional-file-descriptors) output and [IPC messages](ipc.md), separated with newlines and not [interleaved](output.md#interleaved-output).

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 82why, one step back
82

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 83why, one step back
83[`error.shortMessage`](api.md#errorshortmessage) is the same but without `stdout`, `stderr` nor IPC messages.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 84why, one step back
84

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 85why, one step back
85[`error.originalMessage`](api.md#errororiginalmessage) is the same but also without the command. This exists only in specific instances, such as when calling [`subprocess.kill(error)`](termination.md#error-message-and-stack-trace), using the [`cancelSignal`](termination.md#canceling) option, passing an invalid command or [option](api.md#options-1), or throwing an exception in a [stream](streams.md) or [transform](transform.md).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 86–110why, one step back
86
87```js88try {89 await execa`npm run build`;90} catch (error) {91 console.error(error.originalMessage);92 // The task "build" does not exist.9394 console.error(error.shortMessage);95 // Command failed with exit code 3: npm run build96 // The task "build" does not exist.9798 console.error(error.message);99 // Command failed with exit code 3: npm run build100 // The task "build" does not exist.101 // [stderr contents...]102 // [stdout contents...]103}104```105106<hr>107108[**Next**: 🏁 Termination](termination.md)\109[**Previous**: 🌐 Environment](environment.md)\110[**Top**: Table of contents](../readme.md#documentation)