Skip to content

collection.find() exits without throwing any error when user not found #343

@adams-family

Description

@adams-family

I have the following code:

(async () => {
    try {
        const db = require('monk')(
            process.env.MONGO_USERNAME + ':' + 
            process.env.MONGO_PASSWORD + '@' + 
            process.env.MONGO_HOST + '/' + 
            process.env.MONGO_DB);
    
        console.log('STARTING...');
        
        const collection = db.get('collection')

        var result = await collection.find();
        
        console.log('OK')
        
    } catch (error) {
        console.log('ERROR:', error);
        
    } finally {
        console.log('FINALLY')
    }
})();

This is what happens when running nodemon test.js:

[nodemon] starting `node test.js`
STARTING...
[nodemon] clean exit - waiting for changes before restart

This is very strange because the code never hits either one of: OK or ERROR or FINALLY - therefore I suspect that monk.find has to do a process.exit(0) somewhere inside.

In mongo's log I can find that the user was not found:

mongo_1   | {"t":{"$date":"2021-07-25T09:04:24.208+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn34","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","speculative":false,"principalName":"mongo_user","authenticationDatabase":"database","remote":"172.19.0.3:56774","extraInfo":{},"error":"UserNotFound: Could not find user \"mongo_user\" for db \"database\""}}
mongo_1   | {"t":{"$date":"2021-07-25T09:04:24.212+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn33","msg":"Connection ended","attr":{"remote":"172.19.0.3:56772","connectionId":33,"connectionCount":1}}

However, should the code throw an exception in this case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions