Skip to content

Commit 7a0e38b

Browse files
committed
fix(client): fix reviving stack traces in typeson
1 parent 58e6a68 commit 7a0e38b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

client/lib/Handler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { createPromise, pickRandom } from '@secret-agent/commons/utils';
22
import ShutdownHandler from '@secret-agent/commons/ShutdownHandler';
3+
import Log from '@secret-agent/commons/Logger';
34
import IAgentCreateOptions from '../interfaces/IAgentCreateOptions';
45
import IConnectionToCoreOptions from '../interfaces/IConnectionToCoreOptions';
56
import Agent from './Agent';
67
import ConnectionToCore from '../connections/ConnectionToCore';
78
import ConnectionFactory from '../connections/ConnectionFactory';
89

10+
const { log } = Log(module);
11+
912
export default class Handler {
1013
public defaultAgentOptions: IAgentCreateOptions = {};
1114
private readonly connections: ConnectionToCore[] = [];
@@ -99,6 +102,10 @@ export default class Handler {
99102
}
100103

101104
private async logUnhandledError(error: Error): Promise<void> {
105+
// if error and there are remote connections, log error here
106+
if (error && this.connections.some(x => !!x.options.host)) {
107+
log.error('UnhandledRejection', { error, sessionId: null });
108+
}
102109
// eslint-disable-next-line promise/no-promise-in-callback
103110
await Promise.all(this.connections.map(x => x.logUnhandledError(error)));
104111
}

commons/TypeSerializer.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ const buffer = {
1515
},
1616
};
1717

18-
const TSON = new Typeson().register(TypesonRegistry).register(buffer);
18+
const error = {
19+
error: {
20+
test(x) {
21+
return Typeson.toStringTag(x) === 'Error';
22+
},
23+
replace({ name, message, stack }) {
24+
return { name, message, stack };
25+
},
26+
revive({ name, message, stack }) {
27+
let Constructor = Error;
28+
if (global[name]) {
29+
Constructor = global[name];
30+
}
31+
const e = new Constructor(message);
32+
e.name = name;
33+
if (stack) e.stack = stack;
34+
return e;
35+
},
36+
},
37+
};
38+
39+
const TSON = new Typeson().register(TypesonRegistry).register(buffer).register(error);
1940

2041
export default class TypeSerializer {
2142
static stringify(object: any): string {

0 commit comments

Comments
 (0)