2

I have been using MySQL database with Nodejs for a while. I recently switched to MSSQL with Nodejs

When using a try catch block with Nodejs and Mysql any error with the query execution would give me the line at which the error occured

So while using MySQL I did:

try{
   await pool.quer(`sql statement`)
}
catch(err){
  console.log(err)
}

This would give me an error and the line number of error. But with node-mssql, I get:

{ RequestError: Incorrect syntax near 'Invalid'.
    at StreamEvents.req.once.err (C:\Users\David\Main Web\node_modules\mssql\lib\msnodesqlv8\request.js:463:17)
    at Object.onceWrapper (events.js:277:13)
    at StreamEvents.emit (events.js:189:13)
    at errors.forEach.err (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\reader.js:33:20)
    at Array.forEach (<anonymous>)
    at routeStatementError (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\reader.js:26:14)
    at invokeObject.end (C:\Users\David\Main  Web\node_modules\msnodesqlv8\lib\reader.js:258:13)
    at freeStatement (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\driver.js:160:13)
    at cppDriver.freeStatement (C:\Users\David\Main Web\node_modules\msnodesqlv8\lib\driver.js:150:11)
  code: 'EREQUEST',
  originalError:
   { Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'Invalid'. sqlstate: '42000', code: 102 },
  name: 'RequestError',
  number: 102,
  state: undefined }

This is such an unhelpful error. I know where the error is coming from for this error because there is only one sql statement getting executed right now but as my program grows without knowing where the error is coming from it will be hard to debug. Is this an expected behavior of node-mssql?

goxarad784
  • 395
  • 6
  • 17

2 Answers2

0

If you see the documentation here, it clearly mentions what it return in error and what not.

It clearly mentions that "Those errors are initialized in node-mssql module and its original stack may be cropped. You can always access original error with err.originalError"

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
  • `err.originamError` just returns `{ [Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '12'.] sqlstate: '42000', code: 102 }` which is also not very helpful. `err.lineNumber ` returns undefined – goxarad784 Mar 05 '20 at 17:38
0

In your catch block you can try printing the stack trace. That should give you the line number where the error occurred. You can use one of the following statements:

console.stack("---TRACE---")

OR

var stackTrace = new Error().stack
console.log(stackTrace)
jaimish11
  • 536
  • 4
  • 15