Skip to content

Commit

Permalink
Merge pull request #1264 from dhensby/pulls/catch-errors-from-bulk-load
Browse files Browse the repository at this point in the history
Fix handling of errors when adding rows to bulk operations
  • Loading branch information
dhensby committed Jul 6, 2021
2 parents 556ff0e + 8d801da commit 302ae54
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/tedious/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,21 @@ class Request extends BaseRequest {
}

this.parent.acquire(this, (err, connection) => {
if (err) return callback(err)
const callbackWithRelease = (err, ...args) => {
try {
this.parent.release(connection);
} catch (e) {
// noop
}
callback(err, ...args);
}
if (err) return callbackWithRelease(err)

debug('connection(%d): borrowed to request #%d', IDS.get(connection), IDS.get(this))

if (this.canceled) {
debug('request(%d): canceled', IDS.get(this))
this.parent.release(connection)
return callback(new RequestError('Canceled.', 'ECANCEL'))
return callbackWithRelease(new RequestError('Canceled.', 'ECANCEL'))
}

this._cancel = () => {
Expand Down Expand Up @@ -307,13 +314,12 @@ class Request extends BaseRequest {
connection.removeListener(event, errorHandlers[event])
}

this.parent.release(connection)
hasReturned = true

if (this.stream) {
callback(null, rowCount)
callbackWithRelease(null, rowCount)
} else {
callback(error, rowCount)
callbackWithRelease(error, rowCount)
}
}
}
Expand All @@ -325,7 +331,11 @@ class Request extends BaseRequest {
}

for (const row of table.rows) {
bulk.addRow(row)
try {
bulk.addRow(row)
} catch (e) {
return handleError(true, connection, e)
}
}

if (table.create) {
Expand Down

0 comments on commit 302ae54

Please sign in to comment.