Skip to content
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

fix(NODE-5901): propagate errors to transformed stream in cursor #3985

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export abstract class AbstractCursor<
const transform = options.transform;
const readable = new ReadableCursorStream(this);

return readable.pipe(
const transformedStream = readable.pipe(
new Transform({
objectMode: true,
highWaterMark: 1,
Expand All @@ -351,6 +351,12 @@ export abstract class AbstractCursor<
}
})
);

// Bubble errors to transformed stream, because otherwise no way
// to handle this error.
readable.on('error', err => transformedStream.emit('error', err));

return transformedStream;
}

return new ReadableCursorStream(this);
Expand Down
44 changes: 43 additions & 1 deletion test/integration/node-specific/abstract_cursor.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { expect } from 'chai';
import { once } from 'events';
import * as sinon from 'sinon';
import { Transform } from 'stream';
import { inspect } from 'util';

import { type Collection, type FindCursor, MongoAPIError, type MongoClient } from '../../mongodb';
import {
type Collection,
type FindCursor,
MongoAPIError,
type MongoClient,
MongoServerError
} from '../../mongodb';

describe('class AbstractCursor', function () {
describe('regression tests NODE-5372', function () {
Expand Down Expand Up @@ -233,4 +240,39 @@ describe('class AbstractCursor', function () {
});
});
});

describe('transform stream error handling', function () {
let client: MongoClient;
let collection: Collection;
const docs = [{ count: 0 }];
beforeEach(async function () {
client = this.configuration.newClient();

collection = client.db('abstract_cursor_integration').collection('test');

await collection.insertMany(docs);
});

afterEach(async function () {
await collection.deleteMany({});
await client.close();
});

it('propagates errors to transform stream', async function () {
const transform = new Transform({
transform(data, encoding, callback) {
callback(null, data);
}
});

// MongoServerError: unknown operator: $bar
const stream = collection.find({ foo: { $bar: 25 } }).stream({ transform });

const error: Error | null = await new Promise(resolve => {
stream.on('error', error => resolve(error));
stream.on('end', () => resolve(null));
});
expect(error).to.be.instanceof(MongoServerError);
});
});
});