collection.find() exits without throwing any error when user not found
- Dominant language
- JavaScript
- Stars
- 1.8k
- Forks
- 180
- PR merge metrics
- No merged PRs in 30d
Description
I have the following code:
```js
(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?
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the issue with the supplied async script and MongoDB authentication setup, then start at the collection.find() entry point and trace how connection or authentication failures are handled. Done means the failed authentication reaches the caller's catch or finally block instead of ending silently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100