Search code examples
node.jstedious

How to parse error parameter in request callback?


I'm deliberately triggering an error in a stored procedure under certain conditions which I want to catch in my Node.js API which uses the Tedious package.

Code Snippet from API:

        let request = new Request(sql, (err)=>{
            if (err) {
                sqlerr = err;
                console.log(typeof(err));
                console.log("**RQ-ERROR**", err);
            }
        });

In the callback of the "Request" object above there is an "err" parameter. The "typeof()" returns "object"; however, when I dump it to the console it looks like this:

**RQ-ERROR** { RequestError: Duplicate entry for specified period
    at RequestError (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\tedious\lib\errors.js:32:12)
    at Parser.tokenStreamParser.on.token (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\tedious\lib\connection.js:723:34)
    at emitOne (events.js:96:13)
    at Parser.emit (events.js:188:7)
    at Parser.parser.on.token (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
    at emitOne (events.js:96:13)
    at Parser.emit (events.js:188:7)
    at addChunk (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\readable-stream\lib\_stream_readable.js:297:12)
    at readableAddChunk (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\readable-stream\lib\_stream_readable.js:279:11)
    at Parser.Readable.push (C:\inetpub\wwwroot\PersonnelApps\kudosapi\node_modules\readable-stream\lib\_stream_readable.js:240:10)
  message: 'Duplicate entry for specified period',
  code: 'EREQUEST',
  number: 50000,
  state: 1,
  class: 16,
  serverName: 'PERSODG2LNN52\\SQLEXPRESS',
  procName: 'CreateStatusReport',
  lineNumber: 44 }

This almost looks like a JavaScript object but, as you can see, the "RequestError" data isn't quoted nor is there a comma after the text "240:10)" just before the "message" member. I'm not sure if this is a bug in TDS or if I'm just missing something but I cannot access any of the members as it is. I'd have to convert it to a string and parse it which is fine but isn't very elegant.

Suggestions?


Solution

  • as you can see, the "RequestError" data isn't quoted nor is there a comma after the text "240:10)"

    These are artifacts of the console logging out the error message. You can try it out for yourself with something like the following:

    $ node
    > console.log(new Error('this is an error object!'));
    Error: this is an error object!
        at repl:1:13
        at Script.runInThisContext (vm.js:119:20)
        at REPLServer.defaultEval (repl.js:332:29)
        at bound (domain.js:395:14)
        at REPLServer.runBound [as eval] (domain.js:408:12)
        at REPLServer.onLine (repl.js:639:10)
        at REPLServer.emit (events.js:194:15)
        at REPLServer.EventEmitter.emit (domain.js:441:20)
        at REPLServer.Interface._onLine (readline.js:290:10)
        at REPLServer.Interface._line (readline.js:638:8)
    

    I'm not exactly sure what the desired outcome of this question is, but try inspecting the err.message property rather than using the typeof operator.