Skip to content

Commit

Permalink
swallow exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Sep 21, 2023
1 parent c181b02 commit fc66021
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal fun getPackageInfo(
context.packageManager.getPackageInfo(context.packageName, 0)
}
} catch (e: Throwable) {
config.logger.log("Error getting package info.")
config.logger.log("Error getting package info: $e.")
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyApp : Application() {
debug = true
// flushAt = 5
// flushIntervalSeconds = 5
flushAt = 5
flushAt = 1
}
PostHogAndroid.setup(this, config)

Expand Down
21 changes: 8 additions & 13 deletions posthog-v3/posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public final class com/posthog/PostHog {
}

public final class com/posthog/PostHog$Companion {
public final fun capture (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
public static synthetic fun capture$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V
public final fun alias (Ljava/lang/String;Ljava/util/Map;)V
public static synthetic fun alias$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V
public final fun capture (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public static synthetic fun capture$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V
public final fun close ()V
public final fun flush ()V
public final fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
Expand All @@ -43,17 +45,18 @@ public final class com/posthog/PostHog$Companion {
public final fun optOut ()V
public final fun reloadFeatureFlagsRequest ()V
public final fun reset ()V
public final fun screen (Ljava/lang/String;Ljava/util/Map;)V
public static synthetic fun screen$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V
public final fun setup (Lcom/posthog/PostHogConfig;)V
public final fun with (Lcom/posthog/PostHogConfig;)Lcom/posthog/PostHog;
}

public final class com/posthog/PostHogConfig {
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Ljava/util/List;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getApiKey ()Ljava/lang/String;
public final fun getCachePreferences ()Lcom/posthog/PostHogPreferences;
public final fun getContext ()Lcom/posthog/PostHogContext;
public final fun getDataMode ()Lcom/posthog/PostHogDataMode;
public final fun getDebug ()Z
public final fun getEncryption ()Lcom/posthog/PostHogEncryption;
public final fun getFlushAt ()I
Expand All @@ -73,7 +76,6 @@ public final class com/posthog/PostHogConfig {
public final fun getStoragePrefix ()Ljava/lang/String;
public final fun setCachePreferences (Lcom/posthog/PostHogPreferences;)V
public final fun setContext (Lcom/posthog/PostHogContext;)V
public final fun setDataMode (Lcom/posthog/PostHogDataMode;)V
public final fun setDebug (Z)V
public final fun setEncryption (Lcom/posthog/PostHogEncryption;)V
public final fun setFlushAt (I)V
Expand All @@ -96,13 +98,6 @@ public abstract interface class com/posthog/PostHogContext {
public abstract fun getStaticContext ()Ljava/util/Map;
}

public final class com/posthog/PostHogDataMode : java/lang/Enum {
public static final field ANY Lcom/posthog/PostHogDataMode;
public static final field WIFI Lcom/posthog/PostHogDataMode;
public static fun valueOf (Ljava/lang/String;)Lcom/posthog/PostHogDataMode;
public static fun values ()[Lcom/posthog/PostHogDataMode;
}

public abstract interface class com/posthog/PostHogEncryption {
public abstract fun decrypt (Ljava/io/InputStream;)Ljava/io/InputStream;
public abstract fun encrypt (Ljava/io/OutputStream;)Ljava/io/OutputStream;
Expand Down
127 changes: 75 additions & 52 deletions posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,84 +17,102 @@ public class PostHog private constructor() {

private var config: PostHogConfig? = null

// private var sessionManager: PostHogSessionManager? = null
private var featureFlags: PostHogFeatureFlags? = null
private var api: PostHogApi? = null
private var queue: PostHogQueue? = null

public fun setup(config: PostHogConfig) {
synchronized(lock) {
if (enabled) {
config.logger.log("Setup called despite already being setup!")
return
}
try {
if (enabled) {
config.logger.log("Setup called despite already being setup!")
return
}

val cachePreferences = config.cachePreferences ?: PostHogMemoryPreferences()
config.cachePreferences = cachePreferences
// sessionManager = PostHogSessionManager(cachePreferences)
val serializer = PostHogSerializer(config)
val api = PostHogApi(config, serializer)
val queue = PostHogQueue(config, api, serializer)
val featureFlags = PostHogFeatureFlags(config, api)

val optOut = config.cachePreferences?.getValue("opt-out", defaultValue = false) as? Boolean
optOut?.let {
config.optOut = optOut
}
val cachePreferences = config.cachePreferences ?: PostHogMemoryPreferences()
config.cachePreferences = cachePreferences
val serializer = PostHogSerializer(config)
val api = PostHogApi(config, serializer)
val queue = PostHogQueue(config, api, serializer)
val featureFlags = PostHogFeatureFlags(config, api)

val startDate = Date()
val sendCachedEventsIntegration = SendCachedEventsIntegration(config, api, serializer, startDate)
val optOut = config.cachePreferences?.getValue("opt-out", defaultValue = false) as? Boolean
optOut?.let {
config.optOut = optOut
}

this.api = api
this.config = config
this.queue = queue
this.featureFlags = featureFlags
enabled = true
val startDate = Date()
val sendCachedEventsIntegration = SendCachedEventsIntegration(config, api, serializer, startDate)

config.integrations.add(sendCachedEventsIntegration)
this.api = api
this.config = config
this.queue = queue
this.featureFlags = featureFlags
enabled = true

legacyPreferences(config, serializer)
config.integrations.add(sendCachedEventsIntegration)

queue.start()
legacyPreferences(config, serializer)

config.integrations.forEach {
it.install()
}
queue.start()

if (config.preloadFeatureFlags) {
loadFeatureFlagsRequest()
config.integrations.forEach {
try {
it.install()
} catch (e: Throwable) {
config.logger.log("Integration ${it.javaClass.name} failed to install: $e.")
}
}

if (config.preloadFeatureFlags) {
loadFeatureFlagsRequest()
}
} catch (e: Throwable) {
config.logger.log("Setup failed: $e.")
}
}
}

private fun legacyPreferences(config: PostHogConfig, serializer: PostHogSerializer) {
val cachedPrefs = config.cachePreferences?.getValue(config.apiKey) as? String
cachedPrefs?.let {
serializer.deserializeCachedProperties(it)?.let { props ->
val anonymousId = props["anonymousId"] as? String
val distinctId = props["distinctId"] as? String
try {
serializer.deserializeCachedProperties(it)?.let { props ->
val anonymousId = props["anonymousId"] as? String
val distinctId = props["distinctId"] as? String

anonymousId?.let { anon ->
this.anonymousId = anon
}
distinctId?.let { dist ->
this.distinctId = dist
}
anonymousId?.let { anon ->
this.anonymousId = anon
}
distinctId?.let { dist ->
this.distinctId = dist
}

config.cachePreferences?.remove(config.apiKey)
config.cachePreferences?.remove(config.apiKey)
}
} catch (e: Throwable) {
config.logger.log("Legacy cached prefs: $cachedPrefs failed to parse: $e.")
}
}
}

public fun close() {
synchronized(lock) {
enabled = false
try {
enabled = false

config?.integrations?.forEach {
try {
it.uninstall()
} catch (e: Throwable) {
config?.logger?.log("Integration ${it.javaClass.name} failed to uninstall: $e.")
}
}

config?.integrations?.forEach {
it.uninstall()
queue?.stop()
} catch (e: Throwable) {
config?.logger?.log("Close failed: $e.")
}

queue?.stop()
}
}

Expand Down Expand Up @@ -330,8 +348,6 @@ public class PostHog private constructor() {
queue?.flush()
}

// TODO: If you also want to reset the device_id so that the device will be considered a new device in future events, you can pass true as an argument:
// this does not exist on Android yet
public fun reset() {
if (!isEnabled()) {
return
Expand Down Expand Up @@ -385,8 +401,8 @@ public class PostHog private constructor() {
shared.close()
}

public fun capture(event: String, properties: Map<String, Any>? = null, userProperties: Map<String, Any>? = null) {
shared.capture(event, properties = properties, userProperties = userProperties)
public fun capture(event: String, properties: Map<String, Any>? = null, userProperties: Map<String, Any>? = null, groupProperties: Map<String, Any>? = null) {
shared.capture(event, properties = properties, userProperties = userProperties, groupProperties = groupProperties)
}

public fun identify(distinctId: String, properties: Map<String, Any>? = null, userProperties: Map<String, Any>? = null) {
Expand Down Expand Up @@ -428,6 +444,13 @@ public class PostHog private constructor() {
public fun group(type: String, key: String, groupProperties: Map<String, Any>? = null) {
shared.group(type, key, groupProperties = groupProperties)
}
// TODO: add other methods

public fun screen(screenTitle: String, properties: Map<String, Any>? = null) {
shared.screen(screenTitle, properties = properties)
}

public fun alias(alias: String, properties: Map<String, Any>? = null) {
shared.alias(alias, properties = properties)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class PostHogConfig(
// (30).toDuration(DurationUnit.SECONDS) requires Kotlin 1.6
public var flushIntervalSeconds: Int = 30,

public var dataMode: PostHogDataMode = PostHogDataMode.ANY,
public var encryption: PostHogEncryption? = null,
public val integrations: MutableList<PostHogIntegration> = mutableListOf(),
) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.posthog
import java.io.InputStream
import java.io.OutputStream

// TODO: call PostHogEncryption
public interface PostHogEncryption {
public fun decrypt(inputStream: InputStream): InputStream

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class GsonDateTypeAdapter(private val config: PostHogConfig) : JsonDese
return try {
ISO8601Utils.parse(json?.asString, ParsePosition(0))
} catch (e: Throwable) {
config.logger.log("${json?.asString} isn't a deserializable ISO8601 Date.")
config.logger.log("${json?.asString} isn't a deserializable ISO8601 Date: $e.")
null
}
}
Expand All @@ -28,7 +28,7 @@ internal class GsonDateTypeAdapter(private val config: PostHogConfig) : JsonDese
val dateStr = ISO8601Utils.format(src, true)
JsonPrimitive(dateStr)
} catch (e: Throwable) {
config.logger.log("$src isn't a serializable ISO8601 Date.")
config.logger.log("$src isn't a serializable ISO8601 Date: $e.")
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class GzipRequestInterceptor(private val config: PostHogConfig) : Inter
.method(originalRequest.method, gzip(body))
.build()
} catch (e: Throwable) {
config.logger.log("Failed to gzip the request body.")
config.logger.log("Failed to gzip the request body: $e.")

originalRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okio.BufferedSink
import java.io.IOException
import java.io.OutputStream
import java.util.Date

Expand All @@ -24,7 +25,7 @@ internal class PostHogApi(private val config: PostHogConfig, private val seriali
}
}

@Throws(PostHogApiError::class)
@Throws(PostHogApiError::class, IOException::class)
fun batch(events: List<PostHogEvent>) {
val batch = PostHogBatchEvent(config.apiKey, events)

Expand Down Expand Up @@ -54,7 +55,7 @@ internal class PostHogApi(private val config: PostHogConfig, private val seriali
.build()
}

@Throws(PostHogApiError::class)
@Throws(PostHogApiError::class, IOException::class)
fun decide(properties: Map<String, Any>): Map<String, Any>? {
val map = properties.toMutableMap()
map["token"] = config.apiKey
Expand All @@ -64,7 +65,6 @@ internal class PostHogApi(private val config: PostHogConfig, private val seriali
}

client.newCall(request).execute().use {
// TODO: do we handle 429 differently?
if (!it.isSuccessful) throw PostHogApiError(it.code, it.message, body = it.body)

it.body?.let { body ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import java.lang.RuntimeException
internal class PostHogApiError(
val statusCode: Int,
override val message: String,
body: ResponseBody? = null,
val body: ResponseBody? = null,
) : RuntimeException(message)
Loading

0 comments on commit fc66021

Please sign in to comment.