diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e673e2497..7ff85b3e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * Fix: Clock drift issue when calling DateUtils#getDateTimeWithMillisPrecision (#1557) * Feat: Set mechanism type in SentryExceptionResolver (#1556) * Ref: Prefer snake case for HTTP integration data keys (#1559) +* Fix: Assign lastEventId only if event was queued for submission (#1565) ## 5.1.0-beta.1 diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 1e25bf9548..1bb674dc44 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -486,6 +486,7 @@ public final class io/sentry/Scope { public fun addBreadcrumb (Lio/sentry/Breadcrumb;Ljava/lang/Object;)V public fun addEventProcessor (Lio/sentry/EventProcessor;)V public fun clear ()V + public fun clearAttachments ()V public fun clearBreadcrumbs ()V public fun clearTransaction ()V public fun getContexts ()Lio/sentry/protocol/Contexts; diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 9daf50a9d4..490da48cbc 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -87,6 +87,7 @@ public boolean isEnabled() { assignTraceContext(event); final StackItem item = stack.peek(); sentryId = item.getClient().captureEvent(event, item.getScope(), hint); + this.lastEventId = sentryId; } catch (Exception e) { options .getLogger() @@ -94,7 +95,6 @@ public boolean isEnabled() { SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e); } } - this.lastEventId = sentryId; return sentryId; } diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt index 2195afc283..a1aea67909 100644 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ b/sentry/src/test/java/io/sentry/HubTest.kt @@ -287,6 +287,19 @@ class HubTest { verify(mockClient).captureEvent(eq(event), any(), eq(hint)) } + @Test + fun `when captureEvent is called on disabled hub, lastEventId does not get overwritten`() { + val (sut, mockClient) = getEnabledHub() + whenever(mockClient.captureEvent(any(), any(), anyOrNull())).thenReturn(SentryId(UUID.randomUUID())) + val event = SentryEvent() + val hint = { } + sut.captureEvent(event, hint) + val lastEventId = sut.lastEventId + sut.close() + sut.captureEvent(event, hint) + assertEquals(lastEventId, sut.lastEventId) + } + @Test fun `when captureEvent is called and session tracking is disabled, it should not capture a session`() { val (sut, mockClient) = getEnabledHub()