How to force the `finished` function to be called?
In the lesson "Determining End-of-Stream" we have an implementation using the finished function with the socket stream.
According the explanation, "The stream (socket) is passed to finished as the first argument and the second argument is a callback for when the stream ends for any reason. The first argument of the callback is a potential error object. If the stream were to emit an error event the callback would be called with the error object emitted by that event."
If I understood it correctly, if there is an error emited by the socket, the finished function should be executed. Then **how can I force an error to test it? **
Answers
-
Hi Paulo,
In order to test a solution could be to write a custom string from the client socket, then on the server socket, in the data event listener, get the data and check If the value match with your custom string, if it's the case you can trigger an error in the server socket by calling the destroy method on the socket or by emitting an error event for example.
This will cause the finished method to pass the error as argument of the callback.Hope this help🙏.
0 -
Hey @anthonycd thank you for you response.
I tried this approach you suggested - throwing the Error('all done') - with the code bellow:net.createServer((socket) => { const interval = setInterval(() => { socket.write('beat') }, 1000) socket.on('data', (data) => { if (data.toString() === 'all done') { throw Error('all done') } socket.write(data.toString().toUpperCase()) }) finished(socket, (err) => { console.log('FINISHED!!') if (err) { console.error('there was a socket error', err) } clearInterval(interval) }) }).listen(3000)Note that the finished function has a console.log, but the error "leaks" and crashed the app, and the output, does not show the message "FINISHED!!", that should have been logged. Take a look here:
user@xxx-computer exercises % node end-of-stream/index.js
/xxx-path/labs.node-v20/ch-12/exercises/end-of-stream/index.js:14
throw Error('all done')
^Error: all done
at Socket. (/xxx-path/labs.node-v20/ch-12/exercises/end-of-stream/index.js:14:19)
at Socket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:376:12)
at readableAddChunk (node:internal/streams/readable:349:9)
at Readable.push (node:internal/streams/readable:286:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)Node.js v20.9.0
I assume this implementation is not right, but I did not find a proper one online.
Thanks once again.
0 -
Hi @paulo.barbeiro ,
Instead of throwing the error, you should call the destroy() method on the socket, which will allow Node.js internals to properly pass the error to the finished() method. If you throw an error, you need to wrap the logic of checking the value of the data with a try/catch block. The callback of the data event listener will be called asynchronously, but the error will be thrown synchronously, resulting in an exception. Unfortunately, using a try/catch block will add unnecessary complication to your implementation, and the error thrown won't be passed to the finished() method.
0 -
Emitting an error event on the socket instead of calling the destroy() method will also work fine.
0 -
Thanks @anthonycd!
Yes, after reading your first message, I also try the "return Error" and it worked. Here is the implementation:
net.createServer((socket) => { const interval = setInterval(() => { socket.write('beat') }, 1000) socket.on('data', (data) => { if (data.toString() === 'all done') { // throw Error('all done') // Crashes the app: fails the implementation return Error('all done') // Works just fine. Thanks anthonycd for the reminder of teh callbacks logic // socket.destroy() // Also works. Thanks anthonycd for this solution } socket.write(data.toString().toUpperCase()) }) finished(socket, (err) => { console.log('FINISHED!!') if (err) { console.error('there was a socket error', err) } clearInterval(interval) }) }).listen(3000)0 -
nicely andwered @anthonycd - the misunderstanding here was that throwing in an event listener causes the stream to error, but it doesn't.
0
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 19 LFS256 Class Forum
- 1.4K LFS258 Class Forum
- 165 LFS261 Class Forum
- 26 LFS267 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 105 Mobile Computing
- 18 Android
- 72 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 944 Programming and Development
- 310 Kernel Development
- 616 Software Development
- 976 Software
- 368 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)
