Skip to content
Myr

errors.md

This file

Path

docs/errors.md

121 lines · 4.5 KiB · Markdown

Last changed by

Add user guides (#989)ehmickybefore adoptionbe91ecf

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–121why, 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 failure1011When the subprocess fails, the promise returned by [`execa()`](../readme.md#execafile-arguments-options) is rejected with an [`ExecaError`](../readme.md#execaerror) instance. The `error` has the same shape as successful [results](../readme.md#result), with a few additional [error-specific fields](../readme.md#execaerror). [`error.failed`](../readme.md#resultfailed) is always `true`.1213```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 exceptions2728When the [`reject`](../readme.md#optionsreject) option is `false`, the `error` is returned instead.2930```js31const resultOrError = await execa`npm run build`;32if (resultOrError.failed) {33 console.error(resultOrError);34}35```3637## Exit code3839The subprocess fails when its [exit code](https://en.wikipedia.org/wiki/Exit_status) is not `0`. The exit code is available as [`error.exitCode`](../readme.md#resultexitcode). It is `undefined` when the subprocess fails to spawn or when it was [terminated by a signal](termination.md#signal-termination).4041```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:53- [`error.timedOut`](../readme.md#resulttimedout): [`timeout`](termination.md#timeout) option.54- [`error.isCanceled`](../readme.md#resultiscanceled): [`cancelSignal`](termination.md#canceling) option.55- [`error.isMaxBuffer`](../readme.md#resultismaxbuffer): [`maxBuffer`](output.md#big-output) option.56- [`error.isTerminated`](../readme.md#resultisterminated): [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.5758Otherwise, 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.61- An invalid [option](../readme.md#options) was passed.62- 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 message7778For better [debugging](debugging.md), [`error.message`](../readme.md#errormessage) includes both:79- 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).8182[`error.shortMessage`](../readme.md#errorshortmessage) is the same but without `stdout`/`stderr`.8384[`error.originalMessage`](../readme.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](../readme.md#options), or throwing an exception in a [stream](streams.md) or [transform](transform.md).8586```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)