|
1 | 1 | # Stderr and Error Handling
|
2 | 2 |
|
3 |
| -Standard input and standard output from a process are handled directly as iterable data. There is a third data stream, standard error, that is a bit of an outlier. Standard error is meant to be used either purely for error text from the process or for some combination of logging and errors. |
| 3 | +Standard input and standard output from a process are handled directly as |
| 4 | +iterable data. There is a third data stream, standard error, that is a bit of an |
| 5 | +outlier. Standard error is meant to be used either purely for error text from |
| 6 | +the process or for some combination of logging and errors. |
4 | 7 |
|
5 |
| -We are going to discuss how to handle standard error and how this relates to error handling in the `proc` library. There are examples if you want to skip ahead. |
| 8 | +We are going to discuss how to handle standard error and how this relates to |
| 9 | +error handling in the `proc` library. There are examples if you want to skip |
| 10 | +ahead. |
6 | 11 |
|
7 | 12 | Default behavior of `stderr` and errors:
|
8 | 13 |
|
9 |
| -- all process `stderr` will be written to `Deno.stderr` |
10 |
| -- any exit code other than 0 will throw an [`ExitCodeError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=ExitCodeError) |
11 |
| -- if the process ends due to a signal, it will throw a [`SignalError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=SignalError) |
12 |
| -- an error coming from upstream (`stdin`) will be wrapped in an [`UpstreamError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=UpstreamError) |
| 14 | +- all process `stderr` will be written to `Deno.stderr` |
| 15 | +- any exit code other than 0 will throw an |
| 16 | + [`ExitCodeError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=ExitCodeError) |
| 17 | +- if the process ends due to a signal, it will throw a |
| 18 | + [`SignalError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=SignalError) |
| 19 | +- an error coming from upstream (`stdin`) will be wrapped in an |
| 20 | + [`UpstreamError`](https://deno.land/x/proc@{{gitv}}/mod.ts?s=UpstreamError) |
13 | 21 |
|
14 |
| -While the default behaviors are usually adequate, these can be overridden. There is no standard for standard error, so it may take some effort to get the results you want. |
| 22 | +While the default behaviors are usually adequate, these can be overridden. There |
| 23 | +is no standard for standard error, so it may take some effort to get the results |
| 24 | +you want. |
15 | 25 |
|
16 | 26 | ## Taking Control of Stderr
|
17 | 27 |
|
18 |
| -Demo of recoloring stdout. Timestamps. Show how to write this the short way {...decoratedStderr} or something |
| 28 | +You can capture stderr by defining `fnStderr` in the process options. |
| 29 | +This example adds a timestamp and colors the stderr text red. |
| 30 | + |
| 31 | +```typescript |
| 32 | +const decoratedStderr: ProcessOptions<void> = { |
| 33 | + fnStderr: async (stderr) => { |
| 34 | + for await (const line of stderr.lines) { |
| 35 | + console.error(`${gray(new Date().toISOString())} ${red(line)}`); |
| 36 | + } |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +await run( |
| 41 | + { ...decoratedStderr }, |
| 42 | + "bash", |
| 43 | + "-c", |
| 44 | + ` |
| 45 | + echo "This goes to stderr." >&2 |
| 46 | + echo "This goes to stdout." |
| 47 | + `, |
| 48 | +).toStdout(); |
| 49 | +``` |
19 | 50 |
|
20 | 51 | ## Reinterpreting Process Errors
|
21 | 52 |
|
22 | 53 | Catch and reinterpret exit code error, no stderr scraping.
|
23 | 54 |
|
24 | 55 | ## Throwing Errors based on Stderr
|
25 | 56 |
|
26 |
| -Scrape stderr to throw an error. Simple version. Mention the "contract" with process that all lines of stdout should be printed, or logged, or something - where ever you put it, make sure nothing gets dropped. So error goes at the end, once all lines have been processed. |
| 57 | +Scrape stderr to throw an error. Simple version. Mention the "contract" with |
| 58 | +process that all lines of stdout should be printed, or logged, or something - |
| 59 | +where ever you put it, make sure nothing gets dropped. So error goes at the end, |
| 60 | +once all lines have been processed. |
27 | 61 |
|
28 | 62 | ## Throwing Errors based on Stderr (Advanced)
|
29 | 63 |
|
30 | 64 | Scrape stderr to throw an error. Full version.
|
31 |
| - |
32 |
| - |
|
0 commit comments