Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move from ScopeUtil to InternalSentrySdk
To better reflect how cocoa works and have a more central API for hybrid
SDKs
  • Loading branch information
markushi committed Jul 5, 2023
commit 8b1b62b30c4da1d1a9cb7331dfbf37882ad41a43
10 changes: 5 additions & 5 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ public abstract interface class io/sentry/IntegrationName {
public fun getIntegrationName ()Ljava/lang/String;
}

public final class io/sentry/InternalSentrySdk {
public fun <init> ()V
public static fun serializeScope (Lio/sentry/Scope;)Ljava/util/Map;
}

public final class io/sentry/IpAddressUtils {
public static final field DEFAULT_IP_ADDRESS Ljava/lang/String;
public static fun isDefault (Ljava/lang/String;)Z
Expand Down Expand Up @@ -1212,11 +1217,6 @@ public abstract interface class io/sentry/ScopeCallback {
public abstract fun run (Lio/sentry/Scope;)V
}

public final class io/sentry/ScopeUtil {
public fun <init> ()V
public static fun serialize (Lio/sentry/Scope;)Ljava/util/Map;
}

public final class io/sentry/SendCachedEnvelopeFireAndForgetIntegration : io/sentry/Integration {
public fun <init> (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory;)V
public final fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
Expand Down
39 changes: 39 additions & 0 deletions sentry/src/main/java/io/sentry/InternalSentrySdk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.sentry;

import io.sentry.util.MapObjectWriter;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class InternalSentrySdk {

@NotNull
public static Map<String, Object> serializeScope(@Nullable Scope scope) {
final @NotNull Map<String, Object> data = new HashMap<>();
if (scope == null) {
return data;
}

final @NotNull SentryOptions options = scope.getOptions();
final @NotNull ILogger logger = options.getLogger();
final @NotNull ObjectWriter writer = new MapObjectWriter(data);

try {
writer.name("user").value(logger, scope.getUser());
writer.name("contexts").value(logger, scope.getContexts());
writer.name("tags").value(logger, scope.getTags());
writer.name("extras").value(logger, scope.getExtras());
writer.name("fingerprint").value(logger, scope.getFingerprint());
writer.name("level").value(logger, scope.getLevel());
writer.name("breadcrumbs").value(logger, scope.getBreadcrumbs());
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Could not serialize scope.", e);
return new HashMap<>();
}

return data;
}
}
48 changes: 0 additions & 48 deletions sentry/src/main/java/io/sentry/ScopeUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package io.sentry

import io.sentry.protocol.App
import io.sentry.protocol.User
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import kotlin.test.Test
import kotlin.test.assertTrue

class ScopeUtilTest {
class InternalSentrySdkTest {
@Test
fun `serializing scopes correctly creates map`() {
fun `serializeScope correctly creates top level map`() {
val options = SentryOptions()
val scope = Scope(options)

Expand All @@ -22,7 +24,7 @@ class ScopeUtilTest {
)
scope.setTag("variant", "yellow")

val serializedScope = ScopeUtil.serialize(scope)
val serializedScope = InternalSentrySdk.serializeScope(scope)

assertTrue(serializedScope.containsKey("user"))
assertTrue(serializedScope.containsKey("contexts"))
Expand All @@ -32,4 +34,21 @@ class ScopeUtilTest {
assertTrue(serializedScope.containsKey("level"))
assertTrue(serializedScope.containsKey("breadcrumbs"))
}

@Test
fun `serializeScope returns empty map in case scope is null`() {
val serializedScope = InternalSentrySdk.serializeScope(null)
assertTrue(serializedScope.isEmpty())
}

@Test
fun `serializeScope returns empty map in case scope serialization fails`() {
val scope = mock<Scope>()
val options = SentryOptions()
whenever(scope.options).thenReturn(options)
whenever(scope.user).thenThrow(IllegalStateException("something is off"))

val serializedScope = InternalSentrySdk.serializeScope(scope)
assertTrue(serializedScope.isEmpty())
}
}