Repository navigation
fix: fail the request when a value handler throws, and keep its connection - #1980
Merged
dhensby merged 2 commits intoNov 27, 2026
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Error normalization can itself throw on revoked proxies or objects that cannot be safely inspected, escaping the driver's catch block and leaving requests unsettled.
4 open issues
What changed in this PR
This PR makes value-handler exceptions request-level failures across both SQL Server drivers, allowing requests to finish and return their connections to the pool rather than letting exceptions escape into the driver.
Changes:
- Catch and normalize handler exceptions as
EREQUEST, suppress later rows, and preserve error ordering. - Add unit and integration coverage for failure propagation, streaming, SQL
NULL, and connection reuse. - Share thrown-value formatting and document null-safe handlers and failure behavior.
| File | Description |
|---|---|
test/tedious/tedious.js |
Registers NULL and throwing-handler integration cases. |
test/msnodesqlv8/msnodesqlv8.js |
Registers equivalent integration cases for the native driver. |
test/common/unit.js |
Adds driver-event regression tests for handler failures. |
test/common/tests.js |
Adds shared NULL, failure, and connection-reuse tests. |
README.md |
Documents NULL handling and handler-error behavior. |
lib/utils.js |
Extracts reusable thrown-value formatting. |
lib/tedious/request.js |
Catches conversion failures and tracks ordered request errors. |
lib/shared.js |
Adds shared value-handler error normalization. |
lib/msnodesqlv8/request.js |
Catches conversion failures and suppresses later row emission. |
Review effort: Max
Both drivers call a value handler for a SQL NULL value and give it null. The README did not say so, and its example, `value + 1`, turned a NULL int into 1. The example now checks for null, and an integration test pins the behaviour on both drivers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dhensby
force-pushed
the
claude/wizardly-jennings-63f616
branch
from
November 27, 2026 14:00
5995627 to
e5ed47c
Compare
…ction A value handler runs inside the driver's row event with nothing to catch what it throws, so the throw got out into the driver: - tedious 19 let it escape from its token parser. The process crashed, or, with an uncaughtException listener, the request never settled and kept its pooled connection, so the pool never finished closing. - tedious 20 took it for a token parse failure and closed the connection. A thrown Symbol crashed the process in the RequestError constructor, and an Error with a code of its own kept that code. - msnodesqlv8 emits each value from the promise chain that fetches the rows, which ended the query with the throw as a driver error and lost the rest of the results. On a resume it emits them synchronously, so the throw got out to whatever resumed the stream and left the query stopped part way through its rows. Both drivers now catch it. The request fails the way it does on a server error that does not end the batch: with a RequestError that has the code EREQUEST and keeps what was thrown, made an Error if it is not one, as its originalError. A value that cannot be read, such as a revoked Proxy, becomes the cause of an Error that says so, so nothing is thrown back into the driver. The request calls no more handlers, and builds and keeps no more rows, including any part of a later FOR JSON or FOR XML result. The query still runs to its end and the connection goes back to the pool. A stream emits the error, no more rows, and then done. On tedious the error is added to the request's errors only after tedious's own errors have been matched up with the ones the connection emitted, at the place it came. thrownMessage moves to lib/utils.js, and the error is built by one helper next to the valueHandler map. The README says what happens when a handler throws. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dhensby
force-pushed
the
claude/wizardly-jennings-63f616
branch
from
November 27, 2026 14:03
e5ed47c to
f58ecfc
Compare
dhensby
marked this pull request as ready for review
November 27, 2026 14:03
1 task
|
🎉 This PR is included in version 12.8.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


A value handler (
sql.valueHandler) runs inside the driver's row event, with notry/catch. So a handler that throws gets out into the driver, and what happens next depends on the driver and its version:^19.2.2 || ^20.0.0)uncaughtExceptionlistener, the process crashes (Unhandled 'error' event). With a listener, the request never settles, it keeps its pooled connection, andpool.close()hangs.RequestError(EREQUEST), and the connection goes back to the pool.RequestError, but itscodeis the thrown error's own code if it has one (for exampleERR_INVALID_ARG_TYPE). A thrownSymbolcrashes the process in theRequestErrorconstructor.columnevent is emitted from the promise chain that fetches rows. Its.catchends the query with the throw as if it were a driver error, so the rest of the results are lost. On aresume()the rows are emitted synchronously, so the throw gets out to whatever resumed the stream, and the query stays stopped part way through its rows.What changes
RequestErrorthat has the codeEREQUEST. ItsoriginalErroris what the handler threw. A thrown value that is not anErroris put into anErrorfirst. A thrown value that cannot be read, such as a revokedProxy, becomes thecauseof anErrorthat says so, so nothing is thrown back into the driver.FOR JSONorFOR XMLresult, which it does not keep or parse.EJSONand a destroyedtoReadableStream()already behave.errorevent, emits no morerowevents, and then emitsdone.precedingErrorsstays in order. On tedious, it is added to the error list only after tedious's own errors are matched up with the ones the connection emitted, so that matching still works.thrownMessagemoves fromlib/msnodesqlv8/request.jstolib/utils.js. The error is built by one helper,valueHandlerError, inlib/shared.jsnext to thevalueHandlermap.nullfor SQLNULL. Both drivers already do this, so the example(value) => value + 1turned aNULLint into1. The example now checks fornull.Two things change for users that the operator may want to confirm:
request.cancel()from theerrorlistener.err.codeis now alwaysEREQUEST. Before, it could be the thrown error's own code. msnodesqlv8 already forcedEREQUEST.Live results (Azure SQL Edge, local)
sql.valueHandler.set(sql.TYPES.Int, () => { throw new Error('boom') }), a pool withmax: 1,SELECT 1 AS a, 2 AS b:arrayRowMode/execute/ stream /toReadableStreamuncaughtExceptionlistener, the request hangs with{ available: 0, borrowed: 1 }EREQUESTcontext_infoandconnect_timeof the session are gone)EREQUEST/ stream emitserrorthendoneEREQUEST/ stream emitserrorthendoneI also checked these on both tedious versions, with the same results on each:
error, thendone)precedingErrorsin order)batch()with an output parameterFOR JSONresult after the throw (not emitted)pipe()(the destination is destroyed with the error)Tests
arrayRowMode, callback, execute, batch, stream (query and execute),toReadableStream, non-Errorthrows, values that cannot be read, a coded error, a JSON result after the throw (query and execute), and the order of errors. All 17 fail on master.msnodesqlv8 query eventsharness: the same cases, and a batch-ending error after the throw. All 11 fail on master.test/common/tests.js, wired into both driver suites):value handler that throwsin six modes, on a one-connection pool. Each test checks that the session'sCONTEXT_INFOsurvives, which shows that the connection was kept. On master all six fail: on tedious 20 because the connection is replaced, and on tedious 19 with an uncaught error.value handler is given null for a NULL value.npm testpasses (537 unit tests).npm run test-tediousgives 222 passing and 5 pending on tedious 19.2.2 and on 20.4.3. Three tests fail on both:user defined typesand the two spatial/hierarchyid bulk loads. They fail the same way on master, because Azure SQL Edge has no CLR. The msnodesqlv8 integration tests run only on Windows CI.Not in this PR
query()andexecute(), but it does for the output parameters ofbatch(), which it reads through aselect. msnodesqlv8 reads both through aselectin every case, so its handlers probably do apply to them.🤖 Generated with Claude Code