mssql-cr-layer 3.0.4 → 3.0.5

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.
Files changed (2) hide show
  1. package/package.json +5 -1
  2. package/src/index.js +26 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mssql-cr-layer",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "description": "A MS SQL Server interface layer for common requests",
5
5
  "repository": "andrglo/mssql-cr-layer",
6
6
  "author": {
@@ -34,5 +34,9 @@
34
34
  "debug": "^4.4.3",
35
35
  "lodash.every": "^4.6.0",
36
36
  "mssql": "^12.1.1"
37
+ },
38
+ "overrides": {
39
+ "serialize-javascript": "^7.0.5",
40
+ "diff": "^8.0.3"
37
41
  }
38
42
  }
package/src/index.js CHANGED
@@ -5,6 +5,29 @@ const debug = require('debug')('layer:mssql')
5
5
 
6
6
  const connectionParams = new WeakMap() // Hidden connection parameters
7
7
 
8
+ // Tag a DB error with the SQL + params that produced it, so error-alert
9
+ // consumers can show what actually failed (drivers report code/number/state but
10
+ // never the statement itself). Set-if-absent: the innermost runner wins and
11
+ // outer wrappers never clobber a more specific capture. SQL is truncated so a
12
+ // giant generated statement can't blow past downstream storage caps.
13
+ const MAX_SQL_LENGTH = 4 * 1024
14
+
15
+ function attachSqlToError(err, sql, params) {
16
+ if (!err || typeof err !== 'object') {
17
+ return err
18
+ }
19
+ if (err.sql == null && typeof sql === 'string') {
20
+ err.sql =
21
+ sql.length > MAX_SQL_LENGTH
22
+ ? sql.slice(0, MAX_SQL_LENGTH) + ' …(truncated)'
23
+ : sql
24
+ }
25
+ if (err.params == null && params != null) {
26
+ err.params = params
27
+ }
28
+ return err
29
+ }
30
+
8
31
  module.exports = MssqlCrLayer
9
32
 
10
33
  /**
@@ -167,7 +190,7 @@ MssqlCrLayer.prototype.batch = function(script, options) {
167
190
  })
168
191
  .catch(err => {
169
192
  debug('%s %O', script, err)
170
- throw err
193
+ throw attachSqlToError(err, script)
171
194
  })
172
195
  }
173
196
  )
@@ -207,7 +230,7 @@ MssqlCrLayer.prototype.query = function(statement, params, options) {
207
230
  })
208
231
  .catch(err => {
209
232
  debug('%s %O', statement, err)
210
- throw err
233
+ throw attachSqlToError(err, statement)
211
234
  })
212
235
  })
213
236
  }
@@ -267,7 +290,7 @@ MssqlCrLayer.prototype.query = function(statement, params, options) {
267
290
  })
268
291
  .catch(err => {
269
292
  debug('%s %O', statement, err)
270
- throw err
293
+ throw attachSqlToError(err, statement, params)
271
294
  })
272
295
  }
273
296