Throwing exception on mongoose callback - javascript

I'm developing a Node.js + Express. My database is Mongo, and I'm using Mongoose to connect to this database.
I'm trying to throw an exception in a Mongoose query callback this way:
game.save(function (err) {
if (err) {
throw new app.exception.EntitySaveFailed();
}
});
but when I do it, Node server crashes and shows this stacktrace:
[object Object]
at Promise.<anonymous> (/home/server/routes/api/game.js:219:17)
at Promise.<anonymous> (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
at Promise.resolve (/home/server/node_modules/mongoose/lib/promise.js:108:15)
at Promise.<anonymous> (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
Forever detected script exited with code: 8
Forever restarting script for 9389 time
Listening on port 3000...
When I throw an exception outside a mongoose callback function, all works fine.

When you throw an exception in a node.js callback it goes to the code that actually invoked the callback, not the lexical scope of your code. In this case, that means the Promise library receives the exception and crashes your program. This is why errors are typically reported via the first callback parameter rather than exceptions.

You can add "next" to the params of the function, so when you need to throw an exception you can do something like this:
function saveGame(req, res, next) {
game.save(function (err) {
if (err) {
return next(new app.exception.EntitySaveFailed());
}
});
}

Related

Why nodejs is not allowing me to access other route when I access the root route?

I am using the HTTP module of nodejs to create three routes('/','/about' and the last one treats any other route that is not defined as error route). When I access the root route first and try to access other route nodejs throw an error but when I access the error route or the about the route and try accessing another route it works fine.
Below are the code I wrote and the error nodejs throw
Error
PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
Code
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome Home Dev, You are loved');
}
if(req.url==='/about'){
res.end('This is the about page')
}
res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>
<a href='/'>back to homepage</a>
`
);
});
server.listen(5000);
Change to an if/else so you're only processing one branch of the if per request:
const server = http.createServer((req,res)=>{
if(req.url === '/'){
res.end('Welcome Home Dev, You are loved');
} else if (req.url === '/about') {
res.end('This is the about page')
} else {
res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>
<a href='/'>back to homepage</a>`);
}
});
Or, alternately, you could add a return after each res.send() to stop further execution in your request handler after you send a response. Remember, that just because you call res.send() your function still continues to execute so you need to manage control flow so the other code that sends a response doesn't execute once you've already sent a response.

Write after end error when launch server in nodejs

I am beginner at NodeJS and I'm doing a "NodeJS and Express.js full course" at freecodecamp yt and I copied author code which for him works perfectly, but I got an error.
Code:
const http = require('http')
const server = http.createServer((req, res)=> {
if(req.url === '/') {
res.end('Home Page')
}
if(req.url === '/about') {
res.end('About us')
}
res.end('Error')
})
server.listen(3000, ()=>{
console.log('Server listening...');
})
I don't know why he got home, about and error page when user goes to the wrong page it should throw "Error" text on page, but instead my program is throwing an error in nodeJS program:
events.js:377
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (internal/errors.js:322:7)
at writeAfterEnd (_http_outgoing.js:694:15)
at ServerResponse.end (_http_outgoing.js:815:7)
at Server.<anonymous> (C:\Users\jonat\Desktop\nodejs\app.js:10:5)
at Server.emit (events.js:400:28)
at parserOnIncoming (_http_server.js:900:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:127:17)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:753:7)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
Can someone explain this to me? I would be appreciate. Thank you in advance.
That’s caused by the res.end('Error'), which gets always executed. Try to put it into an else clause, or put a return before each res.end(…)

How to test a promise that connects to RabbitMQ?

Hi I'm using Chai and trying to test a custom function that connects to RabbitMQ passing a wrong host:
connect(host) {
return new Promise((resolve, reject) => {
amqp.connect(host)
.then((conn) => {
resolve(conn);
})
.catch((err) => {
throw new Error(err);
});
});
}
If the connection fail I throw a Error, so I'm testing it like this:
it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async () => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.equal(Error);
});
The connection fails and throws a error but my test is not testing that, simply I got the exception on my terminal:
RabbitMQMailer component.
RabbitMQMailer configuration information.
✓ should test rabbitmqmailer host configuration.
✓ should test rabbitmqmailer queue configuration.
✓ should get rabbitmqmailer empty emailContent value after make a new instance.
✓ should get rabbitmqmailer empty emailContentConsumed value after make a new instance.
✓ resolves
✓ should connect to RabbitMQ service successfully with the correct host. (60ms)
Unhandled rejection Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672
at _amqplib2.default.connect.then.catch.err (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.dal.js:1:11069)
at tryCatcher (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:632:19)
1) shouldnt connect to RabbitMQ service successfully with the wrong host.
I tried catching the exception on a trycatch block but it's the same issue.
EDIT: I got this error on terminal after changing my test to:
it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async (done) => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.be.an.instanceof(Error);
done();
});
(node:18911) UnhandledPromiseRejectionWarning: Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672
at _amqplib2.default.connect.then.catch.err (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.dal.js:1:11475)
at tryCatcher (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:632:19)
(node:18911) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:18911) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
1) shouldnt connect to RabbitMQ service successfully with the wrong host.
1) RabbitMQMailer component.
RabbitMQMailer configuration information.
shouldnt connect to RabbitMQ service successfully with the wrong host.:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.test.js)
You are not properly failing. You forgot about the reject. Do this instead:
connect(host) {
return new Promise((resolve, reject) => {
amqp.connect(host)
.then((conn) => {
resolve(conn);
})
.catch((err) => {
reject(new Error(err)); // Pass the error to reject
});
});
}
In your test, use instanceof to match the Error that is returned:
it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async () => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.be.an.instanceof(Error);
});
Also I don't know what you are using for testing, but if it is jest, then this link might help you properly test the promise.
EDIT: Actually nvm. I see you are using Chai
You need to reject the Promise.
connect(host) {
return new Promise((resolve, reject) => {
amqp.connect(host)
.then((conn) => resolve(conn))
.catch((err) => reject(new Error(err));
});
}
And the test
it('shouldnt connect to RabbitMQ service successfully with the wrong host.', () => {
return rabbitmqmailer.connect('amqp://wronghost')
.then(() => { assert.fail('was not supposed to succeed'); })
.catch((err) => { expect(err).to.be.an.instanceof(Error); })
})
You usually want your tests to be isolated. I would recommend mocking your amqp object and expect that the connect method is being called.
It's important that you try and understand the real connect method before designing the mock
You can use a mocking framework like https://sinonjs.org/
Finally I found the way to test it
it('shouldnt connect to RabbitMQ service successfully with the wrong host.', (done) => {
(async () => {
try {
await rabbitmqmailer.connect('amqp://wronghost');
} catch (error) {
chai.assert.typeOf(error, 'error');
} finally {
done();
}
})();
});
But there's some strange issue, if I change the type like a 'string' for example, it tells me:
(node:26053) UnhandledPromiseRejectionWarning: AssertionError: expected [Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672] to be a string
But ! the test passes successfully :
✓ shouldnt connect to RabbitMQ service successfully with the wrong host.
I don't know why it's happen but it works anyway, thanks for the help.

Catch error on IBM Watson IoT NodeJS client

I'm using the IBM Watson IoT NodeJS client to connect and use IBM Watson IoT.
This works when my object with credentials etc. is correct:
var client = new ibm_watson_iot.IotfGateway(MY-JSON-OBJECT-WITH-CREDENTIALS);
But if credentials is wrong, then I get:
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 1234xyz.messaging.internetofthings.ibmcloud.com 1234xyz.messaging.internetofthings.ibmcloud.com:8883
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
error: Forever detected script exited with code: 1
How do I correctly catch this error in a nice way?
You can always use try/catch block to handler error like that
try{
var client = new ibm_watson_iot.IotfGateway(MY-JSON-OBJECT-WITH-CREDENTIALS);
}
catch(error) {
console.log("Error in connection.. Probably configuration object")
}

JavaScript ES6 - console.log with template literal

I'm writing node's script that connect to the mongo database.
I noticed that printing error response is different by small change in console.log syntax. Below example should be more meaningful.
#!/usr/bin/env node
const mongoose = require("mongoose");
const config = require("./config");
mongoose.connect(config.dbURI, (err) => {
if (err) {
console.log(`${err}`); // First console.log
console.log(err); // Second console.log
} else {
console.log(`Database connection successful`)
}
});
Example output of the first console.log
MongoError: Authentication failed.
Example output of the second console.log
{ MongoError: Authentication failed.
at Function.MongoError.create (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:489:72
at authenticateStragglers (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:435:16)
at Connection.messageHandler (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:469:5)
at Socket.<anonymous> (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/connection.js:321:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:563:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
Where comes from this difference?
${err} (or '' + err for that matter) will interpolate the err object with a string which will also change your Error object into a string - which is equivalent to calling err.toString(). However directly logging err will pass it to the console as an object and display it in that manner.
This is called "implicit coercion" - if you would like to know more about this subject I can highly recommend reading You Don't Know JS: Types & Grammar (Chapter 4: Coercion) for a deep dive.
By interpolating in a template string, the err object is cast to a string, using its own .toString method that is inherited from Error.prototype.toString. The code is equivalent to
console.log(String(err));
console.log(err.toString());
When passing the err object directly to the console.log method, it uses node's inspect function instead, the code is equivalent to
console.log(util.inspect(err));

Categories