Skip to content

Commit a2820a7

Browse files
committed
feat: Add lastEventId
1 parent 33e8d47 commit a2820a7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/hub/src/hub.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export class Hub {
1919
/** Is a {@link Layer}[] containing the client and scope */
2020
private readonly stack: Layer[] = [];
2121

22+
/** Contains the last event id of a captured event. */
23+
private _lastEventId?: string;
24+
2225
/**
2326
* Creates a new instance of the hub, will push one {@link Layer} into the
2427
* internal stack on creation.
@@ -169,7 +172,7 @@ export class Hub {
169172
* @returns The generated eventId.
170173
*/
171174
public captureException(exception: any, hint?: SentryEventHint): string {
172-
const eventId = uuid4();
175+
const eventId = (this._lastEventId = uuid4());
173176
this.invokeClientAsync('captureException', exception, {
174177
...hint,
175178
event_id: eventId,
@@ -186,7 +189,7 @@ export class Hub {
186189
* @returns The generated eventId.
187190
*/
188191
public captureMessage(message: string, level?: Severity, hint?: SentryEventHint): string {
189-
const eventId = uuid4();
192+
const eventId = (this._lastEventId = uuid4());
190193
this.invokeClientAsync('captureMessage', message, level, {
191194
...hint,
192195
event_id: eventId,
@@ -200,13 +203,20 @@ export class Hub {
200203
* @param event The event to send to Sentry.
201204
*/
202205
public captureEvent(event: SentryEvent): string {
203-
const eventId = uuid4();
206+
const eventId = (this._lastEventId = uuid4());
204207
this.invokeClientAsync('captureEvent', event, {
205208
event_id: eventId,
206209
});
207210
return eventId;
208211
}
209212

213+
/**
214+
* @returns The last event id of a captured event.
215+
*/
216+
public lastEventId(): string | undefined {
217+
return this._lastEventId;
218+
}
219+
210220
/**
211221
* Records a new breadcrumb which will be attached to future events.
212222
*

packages/hub/test/lib/hub.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,13 @@ describe('Hub', () => {
271271
hub.captureEvent(event);
272272
expect(spy.mock.calls[0][2]!.event_id).toBeTruthy();
273273
});
274+
275+
test('lastEventId should be the same as last created', () => {
276+
const event: SentryEvent = {
277+
extra: { b: 3 },
278+
};
279+
const hub = new Hub();
280+
const eventId = hub.captureEvent(event);
281+
expect(eventId).toBe(hub.lastEventId());
282+
});
274283
});

0 commit comments

Comments
 (0)