Skip to content

fix(subscribe): catch onChange errors and malformed bodies instead of crashing - #32

Merged
sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/subscribe-handler-errors
Nov 15, 2026
Merged

sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/subscribe-handler-errors

Conversation

@sujal-maheshwari2004

Copy link
Copy Markdown
Member

Fixes #16.

Problem. subscribe() called onChange with no guard inside a fire-and-forget loop. A throwing or rejecting handler, including stub-agent's await client.get() failing during a reload, became an unhandled rejection and Node exited with code 1. A message body of null crashed it the same way. An all-digit legacy hash parsed as a number and was delivered as "".

Change

  • Each delivery runs in its own promise chain. Sync throws and rejections go to a new onError option, subscribe(uri, onChange, { onError }), which defaults to console.error. Deliveries stay concurrent, as before.
  • The read loop itself has a .catch(onError).
  • Any body that isn't an event object (non-JSON, null, a number) is treated as a bare version string.
  • The README API list mentions onError.

The change is backward compatible: existing two-argument calls behave the same, except that they no longer crash.

Tested

  • New test: the first handler call throws, the second rejects, and then null and 12345678 bodies arrive. All four deliveries happen, both errors reach onError, and the versions come through as ["v1","v2","null","12345678"].
  • On main the test fails with the escaped Error: sync-ish handler bug.
  • npm test: 14 of 14 pass.

… crashing

The NATS read loop was a fire-and-forget async IIFE that called onChange
unguarded, so a throwing or rejecting handler became an unhandled rejection
and exited the host process. A body of "null" crashed it the same way, and
an all-digit legacy hash parsed as a number and surfaced as "".

Each delivery now runs in its own promise chain whose errors go to an
onError option (default console.error), the loop itself is caught, and any
body that is not an event object is treated as a bare version string.

Fixes #16
@sujal-maheshwari2004
sujal-maheshwari2004 merged commit eea26af into main Nov 15, 2026
1 check passed
@sujal-maheshwari2004
sujal-maheshwari2004 deleted the fix/subscribe-handler-errors branch November 15, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subscribe(): a throwing onChange or a null event body crashes the process

1 participant