errors.md
This file
Path
docs/errors.md
121 lines · 4.4 KiB · Markdown
Last changed by
Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec
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–38why, one step back2930```js31const resultOrError = await execa`npm run build`;32if (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–56why, one step back53- [`error.timedOut`](api.md#errortimedout): [`timeout`](termination.md#timeout) option.54- [`error.isCanceled`](api.md#erroriscanceled): [`cancelSignal`](termination.md#canceling) option.55- [`error.isMaxBuffer`](api.md#errorismaxbuffer): [`maxBuffer`](output.md#big-output) option.56- [`error.isTerminated`](api.md#erroristerminated): [signal termination](termination.md#signal-termination). This includes the [`timeout`](termination.md#timeout) and [`cancelSignal`](termination.md#canceling) options since those terminate the subprocess with a [signal](termination.md#default-signal). However, this does not include the [`maxBuffer`](output.md#big-output) option.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 57–60why, one step back5758Otherwise, the subprocess failed because either:59- An exception was thrown in a [stream](streams.md) or [transform](transform.md).60- The command's executable file was not found.Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 61why, one step back61- An invalid [option](api.md#options) was passed.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 62–77why, one step back62- There was not enough memory or too many subprocesses.6364```js65try {66 await execa`npm run build`;67} catch (error) {68 if (error.timedOut) {69 handleTimeout(error);70 }7172 throw error;73}74```7576## Error message77Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 78why, one step back78For better [debugging](debugging.md), [`error.message`](api.md#errormessage) includes both:Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 79–81why, one step back79- The command and the [reason it failed](#failure-reason).80- Its [`stdout`, `stderr`](output.md#stdout-and-stderr) and [other file descriptors'](output.md#additional-file-descriptors) output, separated with newlines and not [interleaved](output.md#interleaved-output).81Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 82why, one step back82[`error.shortMessage`](api.md#errorshortmessage) is the same but without `stdout`/`stderr`.Add user guides (#989)ehmickybefore adoptionbe91ecf
line 83why, one step back83Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 84why, one step back84[`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 85–121why, one step back8586```js87try {88 await execa`npm run build`;89} catch (error) {90 console.error(error.originalMessage);91 // The task "build" does not exist.9293 console.error(error.shortMessage);94 // Command failed with exit code 3: npm run build95 // The task "build" does not exist.9697 console.error(error.message);98 // Command failed with exit code 3: npm run build99 // The task "build" does not exist.100 // [stderr contents...]101 // [stdout contents...]102}103```104105## Retry on error106107Safely handle failures by using automatic retries and exponential backoff with the [`p-retry`](https://github.com/sindresorhus/p-retry) package.108109```js110import pRetry from 'p-retry';111import {execa} from 'execa';112113const run = () => execa`curl -sSL https://sindresorhus.com/unicorn`;114console.log(await pRetry(run, {retries: 5}));115```116117<hr>118119[**Next**: 🏁 Termination](termination.md)\120[**Previous**: 🌐 Environment](environment.md)\121[**Top**: Table of contents](../readme.md#documentation)