Closed HaKr closed 5 years ago
Could it be that you forget to await/return the result of writable.write?
Same for writable.end
I did indeed came up with
this.target.write( line.length < 1 ? ' ' : line )
.catch( _ => { console.log( 'caught error', _ ), this.target.end( _ ) } )
} )
Still trying to figure out why in this case the throw
bubbles up into this Node stream, while in the Stream.from() variant it triggers the src.aborted().catch()
How would I return the result? (The code above awaits it?)
@HaKr this behavior is by design: The docs from Stream.from()
refer to Stream.writeEach()
for error handling, which says:
If writing of a value fails (either by the `writer` throwing an error,
returning a rejection, or the write call failing), the stream is aborted
and ended with that error.
In your case (as Rogier mentioned), you didn't handle any error from write nor end, which causes the unhandled rejection to show up.
It is up to you how to handle the error in that case. Maybe you just want to 'ignore' it and retry with different value (without aborting the stream), or maybe you want to end the stream with or without an error, or you want to abort it.
If you want to have similar (and easy) behavior as Stream.from()
, your best bet is probably to indeed call Stream.abort(yourError)
.
Be sure to always properly end()
the stream too, just calling abort()
is not enough. This ensures any other stream elements can properly dispose of any resources (such as file handles).
Maybe take a look at https://github.com/poelstra/ts-stream/blob/v2.0.0/src/test/test-idiomatic.ts#L141-L178 for some more inspiration on creating a source yourself, and handling the sometimes tricky cases of proper cleanup.
I was struggling a little with the error handling of a pipeline. Based on the examples I finally got it working. That is, on the test code, but not the real life version. The only difference is that the test code used a Stream.from(a: string[]), whereas the real code opens files.
The code below consolidates the main parts of my code, and demonstrates the different error handling when only a different source stream is used. The file test.dat contains five lines which are the same as the array elements in Stream.from().
When the file based stream is used, Node signals a
(node:12648) UnhandledPromiseRejectionWarning
and none of the result().catch() or aborted().catch() callbacks are activated.