Repository navigation
keyv - fix: follow the EventEmitter error rule and remove throwOnErrors - #2155
Conversation
throwOnErrors never did what it promised. Keyv passed it to Hookified as throwOnEmitError, which Hookified only reads when an error event has no listeners, and Keyv already throws in that case. It could not be made to work across adapters either: most adapters catch their own failures, emit error, and return a fallback value, so the option would have needed a throw mode in each of them. Keyv now follows the Node.js EventEmitter rule and nothing else. When an operation fails, Keyv emits error. With a listener attached, the call returns a fallback value. With none, it rejects. The throwOnErrors option, getter, setter, and type are removed, and Keyv no longer passes a throw setting to Hookified. getMany, getRaw, and getManyRaw now catch store errors like get does. They emit error, record stat:error, and return undefined values, so they follow the same rule instead of rejecting even when a listener is attached. @keyv/redis createKeyv and createKeyvNonBlocking no longer set the removed option. The Keyv README, the website error, options, and properties pages, the v5 to v6 migration guide, and the Redis README now describe the rule, the fallback values, and what changed from v5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5LJCvt3pkR7FtfyAdzg5x
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2155 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 55
Lines 5284 5283 -1
Branches 857 852 -5
=========================================
- Hits 5284 5283 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f90f62f39b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Addresses the review findings on the listener rule: - iterator() follows the rule. When the store fails mid-iteration, Keyv emits error and records stat:error, then ends the iteration if a listener is attached, or rejects if none is. - A failed read no longer counts as a miss. get, getMany, getRaw, and getManyRaw record stat:error only, which matches the documented meaning of stat:miss: a missing or expired key. - @keyv/redis reports each failure once. When it rejects because of throwOnErrors or throwOnConnectError, it no longer also emits error. Keyv forwards the adapter's error events, so a Keyv listener was called twice for one failure. A failed connect now rejects with the connection error as its cause instead of emitting it first. - The migration guide's v5 table now shows that has, hasMany, getMany, and getManyRaw depended on whether the adapter had its own version of the method, and that iterator rejected. - The docs cover iterator's fallback, how failed reads count in stats, and the adapter rule to either reject or emit, not both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5LJCvt3pkR7FtfyAdzg5x
Please check if the PR fulfills these requirements
throwOnErrorstests intest/keyv.test.tsare replaced with tests of the listener rule for all eleven operations, plus tests foriteratorand for how failed reads count in stats. The twosetRaw/setManyRawtests now check the no-listener rejection. New Redis tests check that each failure is reported once.What kind of change does this PR introduce? Bug fix, and removal of the
throwOnErrorsoption. This supersedes #2154.Problem
throwOnErrorsnever did what it promised. Keyv passed it to Hookified asthrowOnEmitError, which Hookified only reads when anerrorevent has no listeners. In that case Keyv already throws, throughthrowOnEmptyListeners. With a listener attached, the option did nothing.It couldn't be fixed only in Keyv either. DynamoDB, Cloudflare KV, etcd, Memcache, and Redis (by default) catch their own failures, emit
error, and return a fallback value. For those adapters, #2154's fix still returnedundefinedwith the option on and a listener attached. Making the option work would have needed a throw mode in every adapter.getMany,getRaw,getManyRaw, anditeratorhad the opposite problem. They didn't catch store errors, so they rejected even with a listener attached, and they emitted noerrorevent.Changes
EventEmitterone. When an operation fails, Keyv emitserror. With a listener attached, the call returns a fallback value. With none, it rejects. This is whatmainalready does for most methods.throwOnErrorsremoved: the option, getter, setter, and type are gone. Keyv no longer passes a throw setting to Hookified, so a future change to Hookified'sthrowOnEmitErrorcan't change Keyv's behavior.getMany,getRaw,getManyRawcatch store errors the waygetdoes. They emiterror, recordstat:error, and returnundefinedvalues.iteratorfollows the same rule. If the store fails mid-iteration, the iteration ends when a listener is attached, and rejects when none is.get, recordstat:erroronly, which matches the documented meaning ofstat:miss.@keyv/redis:createKeyvandcreateKeyvNonBlockingno longer setkeyv.throwOnErrors. Those assignments never changed behavior.throwOnErrorsorthrowOnConnectError, it no longer also emitserror. Before, a Keyv listener got the forwarded event and then the rejection, so it was called twice.cause.throwOnErrorssections are removed. The adapter contract now asks adapters to either reject or emit, not both.throwOnErrorsWas Removed" section, with a quick-guide item. It explains how v5 behaved, method by method, including the methods whose behavior depended on the adapter, and what to change.throwOnErrorsentries are removed from the options and properties pages.Breaking changes
throwOnErrors: the option no longer exists, so passing it is a type error. JavaScript callers see no change, because the option never took effect with a listener attached. Without one, calls already rejected.@keyv/redisusers with an adapter-levelerrorlistener: when the adapter rejects, the listener no longer receives that failure too. The rejection carries it.Verification
pnpm testincore/keyvpasses: lint, 330 tests, and the same coverage asmain. The only partial branches are the four thatmainalready has.cluster.test.tsandsentinel.test.tsneed services that aren't available locally, so CI covers them. Biome is clean.tscreports the same 6 client-typing errors insrc/index.tsasmain.iterator, and the three new Redis tests, fail on the previous commit and pass on this one.tscpasses forcore/keyvsrc. The changed test files add no type errors.pnpm website:buildsucceeds. The new anchors exist, and the links from the migration guide, the events page, and the READMEs resolve.🤖 Generated with Claude Code
https://claude.ai/code/session_01X5LJCvt3pkR7FtfyAdzg5x
Generated by Claude Code