how can make mongoose fail when executing find query - javascript

Hi everyone I'm writing mocha unit tests for my server. How can I get error for mongoose find query. I've tried close the connection before execute but there's nothing firing.
User.find({}, (err, result) => {
if (err) {
// I want to get here
}
return done(result);
});

The following DO NOT WORK with mongoose, at least for now (5.0.17) :
Closing the connection to mongoose is a way to test it, in addition to a proper timeout to set on the find request.
const request = User.find({});
request.maxTime(1000);
request.exec()
.then(...)
.catch(...);
or
User.find({}, { maxTimeMS: 1000 }, (err, result) => {
if (err) {
// I want to get here
}
return done(result);
});
EDIT after further researches :
After trying it myself, it seems that I never get an error from the request.
Changing request maxTime or connection parameters auto_reconnect, socketTimeoutMS, and connectTimeoutMS do not seems to have any effect. The request still hang.
I've found this stack overflow answer saying that all request are queued when mongoose is disconnected from the database. So we won't get any timeout from there.
A soluce I can recommand and that I use on my own project for another reason would be to wrap the mongoose request into a class of my own. So I could check and throw an error myself in case of disconnected database.

In my opinion, the best way to test your error handling is to use mock. More information in this previous stackoverflow topic.
You can mock the mongoose connection and api to drive your test (raise errors...).
Libraries:
sinonjs
testdouble

I solved it like below. Here is the solution.
User = sinon.stub(User.prototype, 'find');
User.yields(new Error('An error occured'), undefined);
By this code it will return error. #ormaz #grégory-neut Thanks for the help.

Related

"Cast to ObjectId failed for value ...." error. Wanting to intercept to create my own error message but getting confused

I just graduated from a Full Stack Bootcamp and this is my first post. I am still trying to learn and apply all of this stuff. Working on a portfolio project for a server using NodeJs/MongoDB/Mongoose and am trying to figure out how to intercept the .catch error and generate my own error message. I'm making a PUT request using an object ID for a specific record and everything is working when a valid ID is passed. However, if I test using an invalid ID it goes straight to catch error and bypasses my handling of this type of scenario. Here is the code I'm trying to manipulate:
.put((req, res, next) => {
Guitar.findByIdAndUpdate(req.params.guitarId, {$set: req.body}, { new: true })
.then(guitar => {
if (guitar) {
console.log(guitar);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(guitar);
} else {
res.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
res.end(`${req.params.guitarId} does not match any guitars in database`);
}
})
.catch(err => next(err));
})
I assumed that if I tried to pass an invalid ID then if(guitar) would return false, then return my error code. But instead it appears that if an invalid ID is sent then it never gets to that point, instead giving the "Cast to Object Id" failure outside of my if/else code. Am I approaching this incorrectly? Thanks in advance for any insight!!!
The Cast to ObjectId failed is occurring before the find operation is sent to the server, so there is never a chance for the promise to resolve and move on to the .then.
You might try explicitly casting the value to ObjectId inside a try-catch block to handle that specific issue, or add some to the .catch for that error.

Is there a way to mock a property on a request object

I have a Javascript REST api endpoint defined using swagger. In my controller, I am logging the IP address from the incoming request.
This is functioning just fine but now I am trying to add Jest tests for coverage and each time I run my test I am getting an error thrown
module.exports.execute = async function execute (req, res) {
try {
log.info("Start processing request from client IP="+req.connection.remoteAddress);
... do some stuff
log.info("Finished processing request from client IP="+req.connection.remoteAddress);
} catch(err) {
log.error("Error caught in Address controller =>" + err.message);
utils.writeJson(res, err.message, 500);
}
};
When I execute my tests, I am getting Error caught in controller =>Cannot read property 'remoteAddress' of undefined
When I comment out the lines that call req.connection.remoteAddress, all is good and I get coverage but not for those 2 lines.
I am guessing the issue is that the req.connection.remoteAddress is a property and not a function.
Is there a way I can mock the response from this call to return a statis string like 1.1.1.1 ?
Any help is appreciated
After thinking about the question in the first comment from #Will Alexander, I added this (second line) and now all is good. Thank you Will
let mockReq = _.set({},'swagger.params.entity.value', JSON.stringify({ fail: false}));
mockReq.connection = _.set({},'remoteAddress', '1.1.1.1');

Can't set headers after they are sent error while trying to maintain user session for a test with chai request agent

We are currently struggling with a Uncaught Error: Can't set headers after they are sent. error message when trying to chain a user sign in into a test with chai-http.
The test signs in as a user which already exists in the database via our API and then should attempts to GET all items from an existing route. Our current test is below which mirrors very closely the example given on the Chai-HTTP documentation http://chaijs.com/plugins/chai-http/#retaining-cookies-with-each-request.
it('should return all notes on /api/notes GET', function (done) {
agent
.post('/users/register')
.send(user)
.then(function() {
return agent
.get('/api/notes')
.end(function (err, res) {
// expectations
done();
});
});
});
Our stack trace
Uncaught Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
at ServerResponse.header (node_modules/express/lib/response.js:718:10)
at ServerResponse.send (node_modules/express/lib/response.js:163:12)
at ServerResponse.json (node_modules/express/lib/response.js:249:15)
at app/routes/usersRouter.js:16:29
at node_modules/passport/lib/middleware/authenticate.js:236:29
at node_modules/passport/lib/http/request.js:51:48
at pass (node_modules/passport/lib/authenticator.js:287:14)
at Authenticator.serializeUser (node_modules/passport/lib/authenticator.js:289:5)
at IncomingMessage.req.login.req.logIn (node_modules/passport/lib/http/request.js:50:29)
at Strategy.strategy.success (node_modules/passport/lib/middleware/authenticate.js:235:13)
at verified (node_modules/passport-local/lib/strategy.js:83:10)
at InternalFieldObject.ondone (node_modules/passport-local-mongoose/lib/passport-local-mongoose.js:149:24)
This is the function being called on our users router which seems to be raising the error (not raised manually, just raised when using chai)
router.post('/register', function(req, res) {
User.register(new User({ username : req.body.username }), req.body.password, function(err, user) {
if (err) {
res.status(500).json({info: err});
}
passport.authenticate('local')(req, res, function () {
res.status(200).json({info: "success"});
});
});
});
Manually testing this functionality works correctly, the issue seems to purely be down to our test and how it is interacting with passport.
Does anyone have any suggestions or pointers which could be of help?
Is there an err object being passed in the User.register callback? If so, try putting return res.status(500).json({info: err}); so that the passport code will not run. The return will exit the function and will not attempt to set the headers twice.
This all comes back to two different pieces of code both answering the request (res.send or res.json).
Essentially there are two possibilities:
1. Passport is using the res object to answer the request, before you do. Check the docs on how this should be done.
2. You get an error and since you have a bug with a missing return in the error handler the res object is set twice.
This is a late answer but I was just having this exact problem.
Did you try putting your passport.authenticate function inside an else statement?
I found that fixed this problem for me.

How to handle ETIMEDOUT error?

How to handle etimedout error on this call ?
var remotePath = "myremoteurltocopy"
var localStream = fs.createWriteStream("myfil");;
var out = request({ uri: remotePath });
out.on('response', function (resp) {
if (resp.statusCode === 200) {
out.pipe(localStream);
localStream.on('close', function () {
copyconcurenceacces--;
console.log('aftercopy');
callback(null, localFile);
});
}
else
callback(new Error("No file found at given url."), null);
})
There are a way to wait for longer? or to request the remote file again?
What exactly can cause this error? Timeout only?
This is caused when your request response is not received in given time(by timeout request module option).
Basically to catch that error first, you need to register a handler on error, so the unhandled error won't be thrown anymore: out.on('error', function (err) { /* handle errors here */ }). Some more explanation here.
In the handler you can check if the error is ETIMEDOUT and apply your own logic: if (err.message.code === 'ETIMEDOUT') { /* apply logic */ }.
If you want to request for the file again, I suggest using node-retry or node-backoff modules. It makes things much simpler.
If you want to wait longer, you can set timeout option of request yourself. You can set it to 0 for no timeout.
We could look at error object for a property code that mentions the possible system error and in cases of ETIMEDOUT where a network call fails, act accordingly.
if (err.code === 'ETIMEDOUT') {
console.log('My dish error: ', util.inspect(err, { showHidden: true, depth: 2 }));
}
In case if you are using node js, then this could be the possible solution
const express = require("express");
const app = express();
const server = app.listen(8080);
server.keepAliveTimeout = 61 * 1000;
https://medium.com/hk01-tech/running-eks-in-production-for-2-years-the-kubernetes-journey-at-hk01-68130e603d76
Try switching internet networks and test again your code. I got this error and the only solution was switching to another internet.
Edit: I now know people besides me that have had this error and the solution was communicating with the ISP and ask them to chek the dns configuration because the http request were failing. So switching networks definitely could help with this.
That is why I will not delete the post. I could save people a few days of headaches (especially noobs like me).
Simply use a different network. Using a different network solved this issue for me within seconds.

NodeJS Redis Wrong # args for 'get' command

Whenever I send Redis using node_redis a get command in my nodejs app, it tells me that it has the wrong number of arguments. I've tried it with a callback and without and it always says that it's wrong.
var user = redClient.get(user);
//and
var user = redClient.get(user, function(err, result) {
if(err){
console.log(err);
}
});
I don't know if anyone cares, or I missed something in the documentation, but apparently adding redis.print as a callback to every command fixes this. Adding your own callback doesn't do anything.

Categories

Resources