Skip to content

Commit

Permalink
fix(core): Set level in server runtime captureException (#10587)
Browse files Browse the repository at this point in the history
Set `level` property on error event captured via `captureException`, 
analogously to how this is handled in browser SDKs

---------

Co-authored-by: Lukas Stracke <[email protected]>
  • Loading branch information
Zih0 and Lms24 authored Dec 12, 2024
1 parent cdaf852 commit 70653af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
29 changes: 15 additions & 14 deletions packages/browser/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,27 +235,28 @@ describe('SentryBrowser', () => {
await flush(2000);

const event = beforeSend.mock.calls[0]?.[0];
expect(event.level).toBe('error');
expect(event.exception).toBeDefined();
expect(event.exception.values[0]).toBeDefined();
expect(event.exception.values[0]?.type).toBe('Error');
expect(event.exception.values[0]?.value).toBe('test');
expect(event.exception.values[0]?.stacktrace.frames).not.toHaveLength(0);
});

it('should capture a message', () =>
new Promise<void>(resolve => {
const options = getDefaultBrowserClientOptions({
beforeSend: event => {
expect(event.message).toBe('test');
expect(event.exception).toBeUndefined();
resolve();
return event;
},
dsn,
});
setCurrentClient(new BrowserClient(options));
captureMessage('test');
}));
it('should capture a message', done => {
const options = getDefaultBrowserClientOptions({
beforeSend: (event: Event): Event | null => {
expect(event.level).toBe('info');
expect(event.message).toBe('test');
expect(event.exception).toBeUndefined();
done();
return event;
},
dsn,
});
setCurrentClient(new BrowserClient(options));
captureMessage('test');
});

it('should capture an event', () =>
new Promise<void>(resolve => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export class ServerRuntimeClient<
* @inheritDoc
*/
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
return resolvedSyncPromise(eventFromUnknownInput(this, this._options.stackParser, exception, hint));
const event = eventFromUnknownInput(this, this._options.stackParser, exception, hint);
event.level = 'error';

return resolvedSyncPromise(event);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions packages/core/test/lib/serverruntimeclient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,52 @@ describe('ServerRuntimeClient', () => {
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(0);
});
});

describe('captureException', () => {
it('sends an exception event with level error', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);

const sendEnvelopeSpy = jest.spyOn(client, 'sendEnvelope');

client.captureException(new Error('foo'));

expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
expect.objectContaining({
level: 'error',
}),
],
],
]);
});
});

describe('captureMessage', () => {
it('sends a message event with level info', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);

const sendEnvelopeSpy = jest.spyOn(client, 'sendEnvelope');

client.captureMessage('foo');

expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
expect.objectContaining({
level: 'info',
}),
],
],
]);
});
});
});
4 changes: 4 additions & 0 deletions packages/deno/test/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ snapshot[`captureException 1`] = `
},
],
},
level: "error",
platform: "javascript",
sdk: {
integrations: [
Expand Down Expand Up @@ -134,6 +135,7 @@ snapshot[`captureMessage 1`] = `
{
category: "sentry.event",
event_id: "{{id}}",
level: "error",
message: "Error: Some unhandled error",
timestamp: 0,
},
Expand Down Expand Up @@ -204,6 +206,7 @@ snapshot[`captureMessage twice 1`] = `
{
category: "sentry.event",
event_id: "{{id}}",
level: "error",
message: "Error: Some unhandled error",
timestamp: 0,
},
Expand Down Expand Up @@ -281,6 +284,7 @@ snapshot[`captureMessage twice 2`] = `
{
category: "sentry.event",
event_id: "{{id}}",
level: "error",
message: "Error: Some unhandled error",
timestamp: 0,
},
Expand Down

0 comments on commit 70653af

Please sign in to comment.