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

IndexMigrator: fix non blocking migration wrapper promise rejection #77018

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ describe('IndexMigrator', () => {
],
});
});

test('rejects when the migration function throws an error', async () => {
const { client } = testOpts;
const migrateDoc = jest.fn((doc: SavedObjectUnsanitizedDoc) => {
throw new Error('error migrating document');
});

testOpts.documentMigrator = {
migrationVersion: { foo: '1.2.3' },
migrate: migrateDoc,
};

withIndex(client, {
numOutOfDate: 1,
docs: [
[{ _id: 'foo:1', _source: { type: 'foo', foo: { name: 'Bar' } } }],
[{ _id: 'foo:2', _source: { type: 'foo', foo: { name: 'Baz' } } }],
],
});

await expect(new IndexMigrator(testOpts).migrate()).rejects.toThrowErrorMatchingInlineSnapshot(
`"error migrating document"`
);
});
});

function withIndex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,18 @@ describe('migrateRawDocs', () => {

expect(logger.error).toBeCalledTimes(1);
});

test('rejects when the transform function throws an error', async () => {
const transform = jest.fn<any, any>((doc: any) => {
throw new Error('error during transform');
});
await expect(
migrateRawDocs(
new SavedObjectsSerializer(new SavedObjectTypeRegistry()),
transform,
[{ _id: 'a:b', _source: { type: 'a', a: { name: 'AAA' } } }],
createSavedObjectsMigrationLoggerMock()
)
).rejects.toThrowErrorMatchingInlineSnapshot(`"error during transform"`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ function transformNonBlocking(
): (doc: SavedObjectUnsanitizedDoc) => Promise<SavedObjectUnsanitizedDoc> {
// promises aren't enough to unblock the event loop
return (doc: SavedObjectUnsanitizedDoc) =>
new Promise((resolve) => {
new Promise((resolve, reject) => {
// set immediate is though
setImmediate(() => {
resolve(transform(doc));
try {
resolve(transform(doc));
} catch (e) {
reject(e);
}
});
});
}