Node.js traps · Chapter 3 of 16
An 'error' event with no listener
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The event loop
- 3 tasks
- Node.js
- In the all labs plan
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: crash the gate on purpose
Write reproduce() in repro.js. Make two stand-in scanners with createScanner('G1') and createScanner('G2') from scanner.js, and start the gate with startGate([g1, g2]) from gate.js. Then, inside one try and in this order: G1 scans 'T0023-E7', G2 fails with the message 'cable unplugged', G1 scans 'T0023-E8'. If one of them throws, resolve to 'crashed: ' followed by the error's message; if none does, resolve to the gate's scanned() list.
The check runs your reproduce() twice: beside the original gate.js it must resolve to 'crashed: cable unplugged' (the failure nobody listened for took the gate down), and beside a fixed gate.js to ['T0023-E7', 'T0023-E8'] (G1 kept scanning). No real waiting: the stand-in emits at once, and reproduce() must finish within 2 seconds.
- Task 2
gate.js: one scanner fails, the gate keeps scanning
Fix startGate(scanners) in gate.js so a failing scanner is handled on its own. When a scanner emits 'error', nothing may throw: log gate: scanner <id> failed (<the error's message>), reconnecting, set that scanner's status to 'reconnecting' and call its reconnect(). The same must happen every time it fails, not only the first time. When it scans again, its status is 'scanning' again. Other scanners keep scanning, and every scan is still counted.
Keep the scan log line (gate: <id> scanned <code>), scanned() (the codes in the order they came) and status() ({ <id>: 'scanning' or 'reconnecting' }) as they are. The check makes its own scanners (EventEmitters with an id and a reconnect it counts) and fails the same scanner more than once.
- Task 3
gate.test.js: a test that would have caught it
Write a vitest test file, gate.test.js, that would have caught the crash before the scanners went live. The check runs your file three times: beside the original gate.js, where at least one test must fail; beside the fixed gate.js, where every test must pass; and beside a wrong fix that listens for a scanner's first failure only (with once), where at least one test must fail.
So start the gate with two stand-in scanners from scanner.js, make one of them fail twice, and check both sides: failing does not throw, its status is 'reconnecting', its reconnects() is 2, and the other scanner's scans are still in scanned(). Each test must finish within 3 seconds.
What you will learn
- repro.js: crash the gate on purpose
- gate.js: one scanner fails, the gate keeps scanning
- gate.test.js: a test that would have caught it
More in The event loop
Build "An 'error' event with no listener" yourself
Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.