-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
catching errors on streams #14304
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
studentId: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
const iterations = 1000000;
console.log(`begin creating ${iterations} documents`)
console.log(mongoose.connection.collections);
for (let i = 0; i < iterations; i++) {
await Test.create({ studentId: (''+i).repeat(100) });
}
console.log('run query');
const stream = mongoose.connection.collection('tests').find().sort().stream();
console.log('stream', stream);
let i = 0;
stream.on('data', () => { console.log('Doc', ++i); });
stream.on('error', err => console.log(err));
stream.on('end', () => console.log('Done'))
}
run(); |
The issue is the const mongoose = require('mongoose');
const { Transform } = require('stream');
const testSchema = new mongoose.Schema({
studentId: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
const iterations = 10000;
/*console.log(`begin creating ${iterations} documents`)
for (let i = 0; i < iterations; i++) {
await Test.create({ studentId: ('' + i).repeat(5000) });
console.log(i);
}*/
const transform = new Transform({
transform(data, encoding, callback) {
callback(null, data);
},
});
console.log('run query');
const stream = mongoose.connection.collection('tests').find().sort({ studentId: -1 }).stream(/*{ transform }*/);
let i = 0;
stream.on('data', () => { console.log('Doc', ++i); });
stream.on('error', err => console.log('Caught', err));
stream.on('end', () => console.log('Done'));
}
run(); However, uncomment the
We're looking into how to fix this. |
The issue looks like the underlying MongoDB node driver just uses
|
I opened PR in MongoDB Node driver repo, we will see if we can get that merged. In the meantime, use the workaround from my previous post. More info on transformed stream error handling here. |
Prerequisites
Mongoose version
8.1.1
Node.js version
20.x
MongoDB server version
6.x
Typescript version (if applicable)
5.1.6
Description
when using a mongoose stream, I cannot catch an initial error, it always kills the proess with an "uncaught exception".
following (pseudo) code:
throws with
I played around with it and couldn't find any way how to catch this error.
Steps to Reproduce
run a query that results into QueryExceededMemoryLimitNoDiskUseAllowed by simple using some big collection and putting a sort on it.
Expected Behavior
the error should be catchable.
The text was updated successfully, but these errors were encountered: