Skip to content

Commit 0cbc6c5

Browse files
committed
docs
1 parent a6f6f50 commit 0cbc6c5

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ProcessOptions, run } from "../../../mod.ts";
2+
import { gray, red } from "../../../src/deps/colors.ts";
3+
4+
const decoratedStderr: ProcessOptions<void> = {
5+
fnStderr: async (stderr) => {
6+
for await (const line of stderr.lines) {
7+
console.error(`${gray(new Date().toISOString())} ${red(line)}`);
8+
}
9+
},
10+
};
11+
12+
await run(
13+
{ ...decoratedStderr },
14+
"bash",
15+
"-c",
16+
`
17+
echo "This goes to stderr." >&2
18+
echo "This goes to stdout."
19+
`,
20+
).toStdout();

site/src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
- [Input](./process-input.md)
1212
- [Stderr and Error Handling](./process-stderr.md)
1313

14-
1514
# Input/Output
1615

1716
- [Reading Stuff]()

site/src/process-stderr.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
11
# Stderr and Error Handling
22

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.
47

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.
611

712
Default behavior of `stderr` and errors:
813

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)
1321

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.
1525

1626
## Taking Control of Stderr
1727

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+
```
1950

2051
## Reinterpreting Process Errors
2152

2253
Catch and reinterpret exit code error, no stderr scraping.
2354

2455
## Throwing Errors based on Stderr
2556

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.
2761

2862
## Throwing Errors based on Stderr (Advanced)
2963

3064
Scrape stderr to throw an error. Full version.
31-
32-

src/process.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,18 @@ export class Process<S> implements Deno.Closer {
270270
try {
271271
let error: Error | undefined;
272272
try {
273-
yield* process.stdout;
273+
let status: Deno.CommandStatus;
274+
try {
275+
yield* process.stdout;
276+
} finally {
277+
status = await process.status;
278+
if (ser != null) {
279+
await ser;
280+
}
281+
}
274282

275-
const status = await process.status;
276283
const cause = passError();
277284

278-
/* Allows error in custom fnStderr function to throw. */
279-
await ser;
280-
281285
if (status.signal != null) {
282286
throw new SignalError(
283287
`signal error: ${status.signal}`,

0 commit comments

Comments
 (0)