generated from alex/base-template
fix: handle @irc/client connect() returning null + reconnect on disconnect #10
No reviewers
Labels
No labels
dependencies
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
alex/irc-greeter-bot!10
Loading…
Reference in a new issue
No description provided.
Delete branch "pi/issue7-1784658236137"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Root cause
The previous retry logic (
retryForever) only caught thrown errors. But@irc/client'sClient.connect()catches connection errors internally and returnsnullinstead of throwing:So when the IRC server was unreachable:
connect()resolved withnull(the error was only emitted as an event)retryForeversaw a resolved promise → treated it as success → returnedstart-limit-hitThis also explains the confusing log output where "Connected" appeared before the error.
Fix
Three changes in
src/main.ts:1. Check
connect()return value fornullThis converts the silent
nullreturn into a thrown error soretryForeveractually retries.2. Wrap the entire connection lifecycle in
retryForeverPreviously,
retryForeveronly covered the initialconnect()call. Now the operation also includesawait client.once("disconnected")— it blocks until the connection drops, then throws"Disconnected from server"to trigger a reconnection with backoff. This handles the "IRC server upgrade" scenario where an established connection is lost.3. Restructured SIGINT handler
The shutdown handler now uses a mutable
currentClientreference (via closure) instead of receiving a fixedClient, so it can always reach the live connection across reconnections. TheshuttingDownflag doubles as a re-entrancy guard and a signal for the retry loop to stop.Test added
A new test documents the
@irc/clientquirk — an operation that resolves withnull(simulatingconnect()failure) must be detected and converted to a thrown error forretryForeverto engage.Fixes #7