Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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);
});
});
});