Skip to content

Commit c79ce54

Browse files
authored
fix(node): save string exception as message of synthetic (#2837)
Closes #2830 Throwing a string in node causes the exception to appear as 'Sentry syntheticException' in Sentry UI. This saves the string as a message of the syntheticException so that the correct message is reported.
1 parent 341c968 commit c79ce54

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/node/src/backend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
7474
// This handles when someone does: `throw "something awesome";`
7575
// We use synthesized Error here so we can extract a (rough) stack trace.
7676
ex = (hint && hint.syntheticException) || new Error(exception as string);
77+
(ex as Error).message = exception;
7778
}
7879
mechanism.synthetic = true;
7980
}

packages/node/test/index.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('SentryNode', () => {
102102
});
103103

104104
test('capture an exception', done => {
105-
expect.assertions(5);
105+
expect.assertions(6);
106106
getCurrentHub().bindClient(
107107
new NodeClient({
108108
beforeSend: (event: Event) => {
@@ -111,6 +111,7 @@ describe('SentryNode', () => {
111111
expect(event.exception!.values![0]).not.toBeUndefined();
112112
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
113113
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
114+
expect(event.exception!.values![0].value).toEqual('test');
114115
done();
115116
return null;
116117
},
@@ -127,6 +128,33 @@ describe('SentryNode', () => {
127128
}
128129
});
129130

131+
test('capture a string exception', done => {
132+
expect.assertions(6);
133+
getCurrentHub().bindClient(
134+
new NodeClient({
135+
beforeSend: (event: Event) => {
136+
expect(event.tags).toEqual({ test: '1' });
137+
expect(event.exception).not.toBeUndefined();
138+
expect(event.exception!.values![0]).not.toBeUndefined();
139+
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
140+
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
141+
expect(event.exception!.values![0].value).toEqual('test string exception');
142+
done();
143+
return null;
144+
},
145+
dsn,
146+
}),
147+
);
148+
configureScope((scope: Scope) => {
149+
scope.setTag('test', '1');
150+
});
151+
try {
152+
throw 'test string exception';
153+
} catch (e) {
154+
captureException(e);
155+
}
156+
});
157+
130158
test('capture an exception no pre/post context', done => {
131159
expect.assertions(10);
132160
getCurrentHub().bindClient(

0 commit comments

Comments
 (0)