mssql 12.7.3 → 12.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -920,6 +920,7 @@ request.execute('procedure_name', (err, result) => {
920
920
 
921
921
  __Errors__
922
922
  - EREQUEST (`RequestError`) - *Message from SQL Server*
923
+ - EPARAM (`RequestError`) - A parameter value is not valid for its type.
923
924
  - EINJECT (`RequestError`) - Procedure name or parameter type is not a valid identifier. See [Identifiers](#identifiers).
924
925
  - ECANCEL (`RequestError`) - Cancelled.
925
926
  - ETIMEOUT (`RequestError`) - Request timeout.
@@ -1123,6 +1124,7 @@ request.query('select 1 as number', (err, result) => {
1123
1124
  __Errors__
1124
1125
  - ETIMEOUT (`RequestError`) - Request timeout.
1125
1126
  - EREQUEST (`RequestError`) - *Message from SQL Server*
1127
+ - EPARAM (`RequestError`) - A parameter value is not valid for its type.
1126
1128
  - ECANCEL (`RequestError`) - Cancelled.
1127
1129
  - ENOCONN (`RequestError`) - No connection is specified for that request.
1128
1130
  - ENOTOPEN (`ConnectionError`) - Connection not yet open.
@@ -1169,6 +1171,7 @@ request.batch('create procedure #temporary as select * from table', (err, result
1169
1171
  __Errors__
1170
1172
  - ETIMEOUT (`RequestError`) - Request timeout.
1171
1173
  - EREQUEST (`RequestError`) - *Message from SQL Server*
1174
+ - EPARAM (`RequestError`) - A parameter value is not valid for its type.
1172
1175
  - ECANCEL (`RequestError`) - Cancelled.
1173
1176
  - ENOCONN (`RequestError`) - No connection is specified for that request.
1174
1177
  - ENOTOPEN (`ConnectionError`) - Connection not yet open.
@@ -1577,6 +1580,7 @@ __Errors__
1577
1580
  - ENOTPREPARED (`PreparedStatementError`) - Statement is not prepared.
1578
1581
  - ETIMEOUT (`RequestError`) - Request timeout.
1579
1582
  - EREQUEST (`RequestError`) - *Message from SQL Server*
1583
+ - EPARAM (`RequestError`) - A parameter value is not valid for its type.
1580
1584
  - ECANCEL (`RequestError`) - Cancelled.
1581
1585
 
1582
1586
  ---------------------------------------
@@ -14,6 +14,16 @@ const JSON_COLUMN_ID = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B'
14
14
  const XML_COLUMN_ID = 'XML_F52E2B61-18A1-11d1-B105-00805F49916B'
15
15
  const EMPTY_BUFFER = Buffer.alloc(0)
16
16
 
17
+ // String() converts a Symbol, which only fails to convert implicitly; an object without a
18
+ // working toString falls back to its tag
19
+ const thrownMessage = function (value) {
20
+ try {
21
+ return String(value)
22
+ } catch (e) {
23
+ return Object.prototype.toString.call(value)
24
+ }
25
+ }
26
+
17
27
  const castParameter = function (value, type) {
18
28
  if (value == null) {
19
29
  if ((type === TYPES.Binary) || (type === TYPES.VarBinary) || (type === TYPES.Image)) {
@@ -417,22 +427,16 @@ class Request extends BaseRequest {
417
427
  }
418
428
  }
419
429
 
420
- this.parent.acquire(this, (err, connection, config) => {
421
- if (err) return callback(err)
422
-
423
- debug('connection(%d): borrowed to request #%d', IDS.get(connection), IDS.get(this))
424
-
425
- if (this.canceled) {
426
- debug('request(%d): canceled', IDS.get(this))
427
- this.parent.release(connection)
428
- return callback(new RequestError('Canceled.', 'ECANCEL'))
429
- }
430
-
431
- // values are bound to the markers by position, so a caller that writes its own
432
- // markers passes the parameters in the order it wrote them
433
- const params = []
430
+ // values are bound to the markers by position, so a caller that writes its own
431
+ // markers passes the parameters in the order it wrote them. They are converted before
432
+ // a connection is borrowed, so that a value that cannot be converted is handed back
433
+ // with nothing to release, rather than thrown while a connection is held.
434
+ const params = []
435
+ let converting
436
+ try {
434
437
  if (bound) {
435
438
  for (const param of bound) {
439
+ converting = param.name
436
440
  params.push(castParameter(param.value, param.type))
437
441
  }
438
442
  } else {
@@ -442,10 +446,30 @@ class Request extends BaseRequest {
442
446
  }
443
447
  const param = this.parameters[name]
444
448
  if (param.io === 1 || (param.io === 2 && param.value != null)) {
449
+ converting = param.name
445
450
  params.push(castParameter(param.value, param.type))
446
451
  }
447
452
  }
448
453
  }
454
+ } catch (e) {
455
+ // what was thrown need not be an Error, nor convert to a string implicitly, and may
456
+ // carry a code of its own
457
+ const err = new RequestError(e instanceof Error ? e : thrownMessage(e), 'EPARAM')
458
+ err.code = 'EPARAM'
459
+ err.message = `Validation failed for parameter '${converting}'. ${err.message}`
460
+ return callback(err)
461
+ }
462
+
463
+ this.parent.acquire(this, (err, connection, config) => {
464
+ if (err) return callback(err)
465
+
466
+ debug('connection(%d): borrowed to request #%d', IDS.get(connection), IDS.get(this))
467
+
468
+ if (this.canceled) {
469
+ debug('request(%d): canceled', IDS.get(this))
470
+ this.parent.release(connection)
471
+ return callback(new RequestError('Canceled.', 'ECANCEL'))
472
+ }
449
473
 
450
474
  debug('request(%d): query', IDS.get(this), command)
451
475
 
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "azure",
22
22
  "node-mssql"
23
23
  ],
24
- "version": "12.7.3",
24
+ "version": "12.7.4",
25
25
  "main": "index.js",
26
26
  "type": "commonjs",
27
27
  "repository": {