Created
May 6, 2019 00:13
-
-
Save saadq/6823da7a1485cbb73dae71a83fe9ef33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { PassThrough } = require('stream'); | |
| // This creates a read/write stream and returns it to the user. | |
| // If an error is thrown, the error should be emitted to the output stream. | |
| function getStream() { | |
| const outputStream = new PassThrough(); | |
| try { | |
| doSomething(); | |
| } catch (error) { | |
| outputStream.emit('error', error); // This line seems to throw another error, instead of just sending it to the output stream to be handled. | |
| } | |
| return outputStream; | |
| } | |
| // A function that throws an error | |
| function doSomething() { | |
| throw new Error('Some error is thrown'); | |
| } | |
| /** | |
| * User code | |
| */ | |
| const stream = getStream(); | |
| // This doesn't run because the `outputStream.emit()` call seems to throw another error inside the `catch` block. | |
| stream.on('error', (err) => { | |
| console.error('========================='); | |
| console.error(err); | |
| console.error('========================='); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment