Skip to content

Commit

Permalink
fix(node-runtime-worker-thread): remove function properties before se…
Browse files Browse the repository at this point in the history
…rializing errors COMPASS-5919 (#1762)
  • Loading branch information
baileympearson authored Nov 30, 2023
1 parent cf6d30c commit 010161a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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

0 comments on commit 010161a

Please sign in to comment.