Skip to content

Commit 8b1b62b

Browse files
committed
Move from ScopeUtil to InternalSentrySdk
To better reflect how cocoa works and have a more central API for hybrid SDKs
1 parent 254554a commit 8b1b62b

File tree

4 files changed

+66
-56
lines changed

4 files changed

+66
-56
lines changed

sentry/api/sentry.api

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ public abstract interface class io/sentry/IntegrationName {
655655
public fun getIntegrationName ()Ljava/lang/String;
656656
}
657657

658+
public final class io/sentry/InternalSentrySdk {
659+
public fun <init> ()V
660+
public static fun serializeScope (Lio/sentry/Scope;)Ljava/util/Map;
661+
}
662+
658663
public final class io/sentry/IpAddressUtils {
659664
public static final field DEFAULT_IP_ADDRESS Ljava/lang/String;
660665
public static fun isDefault (Ljava/lang/String;)Z
@@ -1212,11 +1217,6 @@ public abstract interface class io/sentry/ScopeCallback {
12121217
public abstract fun run (Lio/sentry/Scope;)V
12131218
}
12141219

1215-
public final class io/sentry/ScopeUtil {
1216-
public fun <init> ()V
1217-
public static fun serialize (Lio/sentry/Scope;)Ljava/util/Map;
1218-
}
1219-
12201220
public final class io/sentry/SendCachedEnvelopeFireAndForgetIntegration : io/sentry/Integration {
12211221
public fun <init> (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory;)V
12221222
public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.sentry;
2+
3+
import io.sentry.util.MapObjectWriter;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import org.jetbrains.annotations.ApiStatus;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
@ApiStatus.Internal
11+
public final class InternalSentrySdk {
12+
13+
@NotNull
14+
public static Map<String, Object> serializeScope(@Nullable Scope scope) {
15+
final @NotNull Map<String, Object> data = new HashMap<>();
16+
if (scope == null) {
17+
return data;
18+
}
19+
20+
final @NotNull SentryOptions options = scope.getOptions();
21+
final @NotNull ILogger logger = options.getLogger();
22+
final @NotNull ObjectWriter writer = new MapObjectWriter(data);
23+
24+
try {
25+
writer.name("user").value(logger, scope.getUser());
26+
writer.name("contexts").value(logger, scope.getContexts());
27+
writer.name("tags").value(logger, scope.getTags());
28+
writer.name("extras").value(logger, scope.getExtras());
29+
writer.name("fingerprint").value(logger, scope.getFingerprint());
30+
writer.name("level").value(logger, scope.getLevel());
31+
writer.name("breadcrumbs").value(logger, scope.getBreadcrumbs());
32+
} catch (Exception e) {
33+
options.getLogger().log(SentryLevel.ERROR, "Could not serialize scope.", e);
34+
return new HashMap<>();
35+
}
36+
37+
return data;
38+
}
39+
}

sentry/src/main/java/io/sentry/ScopeUtil.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

sentry/src/test/java/io/sentry/ScopeUtilTest.kt renamed to sentry/src/test/java/io/sentry/InternalSentrySdkTest.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package io.sentry
22

33
import io.sentry.protocol.App
44
import io.sentry.protocol.User
5+
import org.mockito.kotlin.mock
6+
import org.mockito.kotlin.whenever
57
import kotlin.test.Test
68
import kotlin.test.assertTrue
79

8-
class ScopeUtilTest {
10+
class InternalSentrySdkTest {
911
@Test
10-
fun `serializing scopes correctly creates map`() {
12+
fun `serializeScope correctly creates top level map`() {
1113
val options = SentryOptions()
1214
val scope = Scope(options)
1315

@@ -22,7 +24,7 @@ class ScopeUtilTest {
2224
)
2325
scope.setTag("variant", "yellow")
2426

25-
val serializedScope = ScopeUtil.serialize(scope)
27+
val serializedScope = InternalSentrySdk.serializeScope(scope)
2628

2729
assertTrue(serializedScope.containsKey("user"))
2830
assertTrue(serializedScope.containsKey("contexts"))
@@ -32,4 +34,21 @@ class ScopeUtilTest {
3234
assertTrue(serializedScope.containsKey("level"))
3335
assertTrue(serializedScope.containsKey("breadcrumbs"))
3436
}
37+
38+
@Test
39+
fun `serializeScope returns empty map in case scope is null`() {
40+
val serializedScope = InternalSentrySdk.serializeScope(null)
41+
assertTrue(serializedScope.isEmpty())
42+
}
43+
44+
@Test
45+
fun `serializeScope returns empty map in case scope serialization fails`() {
46+
val scope = mock<Scope>()
47+
val options = SentryOptions()
48+
whenever(scope.options).thenReturn(options)
49+
whenever(scope.user).thenThrow(IllegalStateException("something is off"))
50+
51+
val serializedScope = InternalSentrySdk.serializeScope(scope)
52+
assertTrue(serializedScope.isEmpty())
53+
}
3554
}

0 commit comments

Comments
 (0)