How to parse error parameter in request callback? - node.js

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?

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.

Related

AWS Lambda Node error logging: Is it necessary to separately log the stack?

Some of the example code in the AWS Javascript SDK docs log the stack of an error separately to the error itself, for example this is from the AWS.CloudWatch overview:
var cloudwatch = new AWS.CloudWatch();
cloudwatch.getMetricWidgetImage(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
So far as I can tell, this is redundant in Node (I have only tested on v10.16). If I log just the error object, it includes the stack trace:
> console.log(e)
Error: x
at repl:2:8
at Script.runInThisContext (vm.js:122:20)
at REPLServer.defaultEval (repl.js:332:29)
at bound (domain.js:402:14)
at REPLServer.runBound [as eval] (domain.js:415:12)
at REPLServer.onLine (repl.js:642:10)
at REPLServer.emit (events.js:198:13)
at REPLServer.EventEmitter.emit (domain.js:448:20)
at REPLServer.Interface._onLine (readline.js:308:10)
at REPLServer.Interface._normalWrite (readline.js:451:12)
undefined
Whereas if I log both, as Amazon have done above, I get an ugly ouput with duplication:
> console.log(e, e.stack)
Error: x
at repl:2:8
at Script.runInThisContext (vm.js:122:20)
at REPLServer.defaultEval (repl.js:332:29)
at bound (domain.js:402:14)
at REPLServer.runBound [as eval] (domain.js:415:12)
at REPLServer.onLine (repl.js:642:10)
at REPLServer.emit (events.js:198:13)
at REPLServer.EventEmitter.emit (domain.js:448:20)
at REPLServer.Interface._onLine (readline.js:308:10)
at REPLServer.Interface._normalWrite (readline.js:451:12) 'Error: x\n at repl:2:8\n at Script.runInThisContext (vm.js:122:20)\n at REPLServer.defaultEval (repl.js:332:29)\n at bound (domain.js:402:14)\n at REPLServer.runBound [as eval] (domain.js:415:12)\n at REPLServer.onLine (repl.js:642:10)\n at REPLServer.emit (events.js:198:13)\n at REPLServer.EventEmitter.emit (domain.js:448:20)\n at REPLServer.Interface._onLine (readline.js:308:10)\n at REPLServer.Interface._normalWrite (readline.js:451:12)'
undefined
Is there some reason that Amazon are using the console.log(err, err.stack) pattern that I am missing if I just do console.log(err)?
(I am primarily using Node in an AWS Lambda, version 10.x)
The console.log(err, err.stack) formula was required for Node versions < 6, as they did not log the error stack by default.
More recent Node versions can just do console.log(err).
See https://stackoverflow.com/a/33593443/1695906 for a bit more detail
(The oldest Node offered by AWS Lambda at the time of writing is Node.js 8.10, so perhaps AWS should update their docs now to remove this.)
(From "#Michael - sqlbot"'s comment, thanks!)

Neo4j's NPM component blows up without error

I'm using the thingdom/node-neo4j module like this:
var neo = require('neo4j');
var db = new new.GraphDatabase(...);
but when I call the cypher method with some invalid script, instead of returning an error (allowing me to handle it), it blows up:
db.cypher('// invalid cypher script', (err, res) => {
if (err) console.log('ERROR: ' + err);
console.log(res);
})
what I get is a stack trace that looks like this:
/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:20
Error.captureStackTrace(this, this.constructor);
^
TypeError: Error.captureStackTrace is not a function
at ClientError.Error [as constructor] (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:20:13)
at new ClientError (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:81:48)
at Function.__dirname.Error.Error._fromObject (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/errors.js:70:14)
at /Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/GraphDatabase.js:302:25
at Request._callback (/Users/ekkis/dev/test/inc/node_modules/neo4j/lib-new/GraphDatabase.js:92:20)
at Request.self.callback (/Users/ekkis/dev/test/inc/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request. (/Users/ekkis/dev/test/inc/node_modules/request/request.js:969:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
I'm using node v7.3.0 and posted an issue with the maintainer here: https://github.com/thingdom/node-neo4j/issues/214 but so far no solution. is anyone else running into this? any solutions?

Error: job x doesnt exist

Is someone know why this issue my appears and how to catch it? I do not remove any jobs manually, use only .removeOnComplete(true) I have some different job processor for the same job task with different parameters. May it cause the issue? How to be in this case?
queue.create('task', jobData)
.removeOnComplete(true)
.priority('normal')
.attempts(3)
.save()
queue.process('task', 5, function (job, ctx, done) {
// First task prcessor
});
queue.process('task', 5, function (job, ctx, done) {
// Second task prcessor
});
Error:
Error: job "425596" doesnt exist
at Command.callback (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/kue/lib/queue/job.js:178:17)
at normal_reply (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/redis/index.js:714:21)
at RedisClient.return_reply (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/redis/index.js:816:9)
at JavascriptRedisParser.Parser.returnReply (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/redis/index.js:188:18)
at JavascriptRedisParser.execute (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/redis-parser/lib/parser.js:415:12)
at Socket.<anonymous> (/home/carmod/www/avitoparser/bundle/programs/server/npm/node_modules/redis/index.js:267:27)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)

Failed to launch renderer:Exception For HTML5 to PDF Convertor

I am using "html5-to-pdf" module to convert HTML5 page to PDF.Below is the code snippet I am using for specifying the Input and Output files.
var html5pdf = require("html5-to-pdf");
var fs = require("fs");
fs.createReadStream("/Vineet/POC/HTML_Inputs/TestSample.html")
.pipe(html5pdf())
.pipe(fs.createWriteStream("/Vineet/POC/PDF_Outputs/Output.pdf"));
While executing this code I am getting below exception:
Error: Failed to launch renderer
at C:\Vineet\POC\node_modules\html5-to-pdf\src\phantom.coffee:55:35
at Proto.apply (C:\Vineet\POC\node_modules\dnode-protocol\index.js:123:13)
at Proto.handle (C:\Vineet\POC\node_modules\dnode-protocol\index.js:99:19)
at D.dnode.handle (C:\Vineet\POC\node_modules\dnode\lib\dnode.js:140:21)
at D.dnode.write (C:\Vineet\POC\node_modules\dnode\lib\dnode.js:128:22)
at SockJSConnection.ondata (stream.js:31:26)
at emitOne (events.js:96:13)
at SockJSConnection.emit (events.js:188:7)
at Session.didMessage (C:\Vineet\POC\node_modules\sockjs\lib\transport.js:220:25)
at WebSocketReceiver.didMessage (C:\Vineet\POC\node_modules\sockjs\lib\trans-websocket.js:102:40)
at C:\Vineet\POC\node_modules\sockjs\lib\trans-websocket.js:75:22
at .<anonymous> (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket\api\event_target.js:41:7)
at Array.forEach (native)
at EventTarget.dispatchEvent (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket\api\event_target.js:40:33)
at API.receive (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket\api.js:30:10)
at instance._emitFrame (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket\hybi_parser.js:285:44)
at instance.parse (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket\hybi_parser.js:143:18)
at Socket.<anonymous> (C:\Vineet\POC\node_modules\faye-websocket\lib\faye\websocket.js:72:33)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:172:18)
at Socket.Readable.push (_stream_readable.js:130:10)
at TCP.onread (net.js:542:20)
Due to which The PDF generated is of 0 bytes and is not being able to open.CAn any one help me in resolving this issue.
This package need PhatomJS installed on your computer. Phantomjs renders the page and saves it to a PDF. Read html5-to-pdf options params
You should use html-pdf package instead.
var pdf = require('html-pdf'),
fs = require('fs');
pdf.create(fs.readFileSync('./Vineet/POC/HTML_Inputs/TestSample.html', 'utf8'), { format: 'Letter' }).toFile('./Vineet/POC/PDF_Outputs/Output.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res);
});

NodeLoad module gives Error : Object #<Client> has no method 'destroy'

I am using NodeLoad module of nodejs for sending multiple request at same time for testing server node app, but if i use numUsers : 50 than it's work perfectly for me. When I take numUsers : 300 than Gives me Error Like : TypeError: Object #<Client> has no method 'destroy'
NodeLoad App I used
timeLimit: 10,
targetRps: 5,
numUsers : 300,
Error :
},reconnect=function(){var oldclient=client;if(oldclient){oldclient.destroy();
^
TypeError: Object #<Client> has no method 'destroy'
at reconnect (/root/nodeLoadDemo/nodeload/node_modules/nodeload/nodeload.js:9:506)
at Client.<anonymous> (/root/nodeLoadDemo/nodeload/node_modules/nodeload/nodeload.js:10:180)
at Client.EventEmitter.emit (events.js:117:20)
at ClientRequest.<anonymous> (http.js:2144:10)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketOnEnd [as onend] (http.js:1568:9)
at Socket.g (events.js:180:16)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:919:16
at process._tickCallback (node.js:419:13)
So, can any one Explain me How Error ? And Solution for these Error ?
Thank you .
I got an Answer
Click Here To go on Solution
You Just need to change in node modules. open nodeload modules and nodeload.js file check line 9 at end of sentence replace
if (oldclient) { oldclient.destroy(); }
TO
if (oldclient && oldclient.destroy) { oldclient.destroy(); }

Resources