Skip to content

fix: give bulk() the last error when the server sends several - #1992

Merged
dhensby merged 2 commits into
tediousjs:masterfrom
kuldeepsinh19:fix/bulk-multiple-errors
Dec 2, 2026
Merged

dhensby merged 2 commits into
tediousjs:masterfrom
kuldeepsinh19:fix/bulk-multiple-errors

Conversation

@kuldeepsinh19

Copy link
Copy Markdown
Contributor

What this does:

On tedious, bulk() rejects with an empty error when SQL Server sends more than one error for the load. The most common case is a table that doesn't exist, and that includes a #temp table created on a different connection of the pool:

// #staging was created on another connection of the pool
const table = new sql.Table('#staging')
table.columns.add('a', sql.Int, { nullable: true })
table.rows.add(1)
await pool.request().bulk(table)
// master:  RequestError with message '' and number undefined
// this PR: RequestError: Invalid object name '#staging'. (number 208)

In that case the server sends "Invalid object name" twice, and tedious 15+ calls the bulk load back with one AggregateError that holds both errors. _bulk() still has the check that #1989 replaced in query() and execute(). It compares the AggregateError's message, which is empty, with the last error it collected, so it adds the AggregateError as one more error and rejects with that. The real errors end up in precedingErrors, and a stream emits an extra error event with an empty message.

The fix is one line: _bulk()'s done now calls _addCallbackError(), as query() and execute() do. That also covers the create table statement, because it reports through the same done. A single error from the server, and errors that tedious makes itself (timeouts, validation), are handled the same as before.

Tests:

  • Unit: adds bulk to the errors from the server (tedious) loop, with newBulkLoad and execBulkLoad on the stub connection. On master, 3 of the 5 cases fail: two errors, two errors with the same message, and the stream. All 5 pass with this change.
  • Integration: adds bulk load into a table that does not exist. Like the other multiple-error tests it's registered for tedious only, because msnodesqlv8 reports a missing table with its own message and never builds an AggregateError.

What I ran:

  • npm test (standard and 579 unit tests) and commitlint both pass.
  • npm run test-tedious against SQL Server 2022 in Docker, with tedious 19.2.2 and with 20.4.3: 230 passing and 5 pending on both. On master the new test is the only failure, with '' !== "Invalid object name 'bulk_table_missing'." on both tedious versions.
  • npm run test-cli passes.
  • Against the server, I also re-ran single-error bulk failures (duplicate key, check constraint, truncation, NOT NULL). Their messages are unchanged.
  • Not run: msnodesqlv8, which this change doesn't touch, and SQL Server 2019 or 2025. The new test checks only the final message and number, so it doesn't depend on how many errors a given server version sends.

Prepared with the help of an AI coding agent.

Related issues:

None found.

Pre/Post merge checklist:

  • Update change log

kuldeepsinh19 and others added 2 commits December 2, 2026 11:33
When the server sends two or more errors for a bulk load, tedious 15+
calls it back with one AggregateError that holds them all. bulk()
compared only the message of the AggregateError, which is empty, with
the last error the connection had emitted, so it added the
AggregateError as one more error.

So bulk() rejected with an error that had an empty message and no
number, and the error from the server was in precedingErrors. This is
what happens for a table that does not exist, such as a temporary table
made on another connection of the pool, where the server sends
"Invalid object name" twice. A stream emitted the AggregateError as an
extra error with an empty message.

bulk() now uses the method that query() and execute() use since
f0bae04, which compares each error in the AggregateError with the error
in the same position.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
bulk() reports an error from its create table statement through the
same callback as the load itself. Each case for errors from the server
now also runs with table.create set, so that the errors come from that
statement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhensby
dhensby force-pushed the fix/bulk-multiple-errors branch from 47617fe to d2e601d Compare December 2, 2026 11:34

@dhensby dhensby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. It makes bulk() match what #1989 did for query() and execute(), and I've checked the fix against a server on tedious 19 and 20.

I've pushed one test commit. The multiple-error cases now also run with table.create set, because the create table statement reports through the same done and can also fail with more than one error.

@dhensby
dhensby merged commit e457654 into tediousjs:master Dec 2, 2026
93 of 94 checks passed
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.

2 participants