Skip to content

fix: prevent TypeError in batch with output parameters when sql errors#1864

Merged
dhensby merged 1 commit into
tediousjs:masterfrom
dhensby:claude/bold-lederberg-d4620a
May 29, 2026
Merged

fix: prevent TypeError in batch with output parameters when sql errors#1864
dhensby merged 1 commit into
tediousjs:masterfrom
dhensby:claude/bold-lederberg-d4620a

Conversation

@dhensby
Copy link
Copy Markdown
Collaborator

@dhensby dhensby commented May 29, 2026

Summary

  • Guard recordsets.pop()[0] in lib/tedious/request.js with optional chaining so a SQL error in a batch with declared output parameters doesn't crash before the request callback fires.
  • Without the guard, when the batch fails before the appended select 1 as [___return___], ... trailer can run (e.g. a compile error, or a runtime error under SET XACT_ABORT ON), recordsets is empty, recordsets.pop() is undefined, and [0] throws synchronously inside the tedious request callback. The user callback never fires, the request promise never settles, and the connection is never released.
  • Added an integration test ('batch with output parameters and sql error') that reproduces the original crash and asserts the SQL error rejects the promise as a RequestError / EREQUEST.

Test plan

  • npm run test-tedious -- --grep 'batch with output parameters and sql error' passes (was crashing with TypeError: Cannot read properties of undefined (reading '0') at request.js:480 on master)
  • All adjacent batch / output / stored-procedure tests still pass
  • npm run lint clean

Fixes #1863

🤖 Generated with Claude Code

When a batch with declared output parameters fails before the appended
`select 1 as [___return___], ...` trailer can run (e.g. a compile error
or a runtime error under SET XACT_ABORT ON), `recordsets` is empty and
`recordsets.pop()[0]` throws synchronously inside the tedious request
callback, preventing the user callback from being invoked and leaving
the request promise unsettled.

Guard the access with optional chaining so the SQL error propagates
through the existing callback path and rejects the promise normally.

Fixes tediousjs#1863

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dhensby dhensby force-pushed the claude/bold-lederberg-d4620a branch from 0e108f5 to 0db36a4 Compare May 29, 2026 19:11
@dhensby dhensby merged commit bcb8267 into tediousjs:master May 29, 2026
51 of 63 checks passed
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 12.5.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Cannot read properties of undefined (reading '0') in batch with OUTPUT parameters when SQL error occurs

1 participant