errors.md
This file
Path
docs/errors.md
110 lines · 4.5 KiB · Markdown
Last changed by
Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–10why, one step back1<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 failure10Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 11why, one step back11When 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 back1213```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 exceptions27Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 28why, one step back28When the [`reject`](api.md#optionsreject) option is `false`, the `error` is returned instead.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 29–30why, one step back2930```jsFix "Preventing exceptions" example (#1110)Karl Horkybefore adoptiona5a4d69
line 31why, one step back31const resultOrError = await execa({reject: false})`npm run build`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 32–38why, one step back32if (resultOrError.failed) {33 console.error(resultOrError);34}35```3637## Exit code38Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec
line 39why, one step back39The 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 back4041```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 back53- [`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 back55- [`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 back56- [`error.isMaxBuffer`](api.md#errorismaxbuffer): [`maxBuffer`](output.md#big-output) option.Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 57why, one step back57- [`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 back5859Otherwise, 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.Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 62why, one step back62- An invalid [option](api.md#options) was passed.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 63–78why, one step back63- 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 message78Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 79why, one step back79For better [debugging](debugging.md), [`error.message`](api.md#errormessage) includes both:Add user guides (#989)ehmickybefore adoptionbe91ecf
line 80why, one step back80- The command and the [reason it failed](#failure-reason).Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 81why, one step back81- 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 back82Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 83why, one step back83[`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 back84Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 85why, one step back85[`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), or throwing an exception in a [stream](streams.md) or [transform](transform.md).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 86–110why, one step back8687```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)