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-runtime-worker-thread): remove function properties before serializing errors COMPASS-5919 #1762

Merged
merged 1 commit into from
Nov 30, 2023
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
44 changes: 44 additions & 0 deletions packages/node-runtime-worker-thread/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,50 @@ describe('WorkerRuntime', function () {
.to.have.property('stack')
.matches(/SyntaxError: Syntax!/);
});

it('COMPASS-5919 - correctly serializes babel parse errors', async function () {
/**
* babel syntax errors have a `clone()` method, which breaks structured cloning
*/
runtime = new WorkerRuntime('mongodb://nodb/', dummyOptions, {
nodb: true,
});

const err: Error = await runtime.evaluate('1 +* 3').catch((e) => e);

expect(err).to.be.instanceof(Error);
expect(err).to.have.property('name', 'SyntaxError');
});

context(
'when `evaluate` returns an error that has a function property',
function () {
it('removes the function property from the error', async function () {
runtime = new WorkerRuntime('mongodb://nodb/', dummyOptions, {
nodb: true,
});

const script = `
class CustomError extends Error {
constructor() {
super('custom error');
}
foo() {
return 'hello, world';
}
}
throw new CustomError();
`;

const err: Error = await runtime.evaluate(script).catch((e) => e);

expect(err).to.be.instanceof(Error);
expect(err).to.have.property('name', 'Error');
expect(err).not.to.have.property('foo');
expect(err).to.have.property('message', 'custom error');
});
}
);
});
});

Expand Down
5 changes: 4 additions & 1 deletion packages/node-runtime-worker-thread/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ function getNames<T>(obj: T): (keyof T)[] {
*/
export function serializeError(err: Error) {
// Name is the only constructor property we care about
const keys = getNames(err).concat('name');
const keys = getNames(err)
.concat('name')
// structured cloning cannot handle functions
.filter((key) => typeof err[key] !== 'function');
return keys.reduce((acc, key) => {
(acc as any)[key] = err[key];
return acc;
Expand Down