amplitude / Amplitude-Kotlin

Amplitude Kotlin SDK
MIT License
27 stars 10 forks source link

Value <html> of type java.lang.String cannot be converted to JSONObject #163

Open niqo01 opened 7 months ago

niqo01 commented 7 months ago

Expected Behavior

No error reported via the LoggerProvider

Current Behavior

The following error message is reported to the LoggerProvider error callback Error when upload event: Value <html> of type java.lang.String cannot be converted to JSONObject

Steps to Reproduce

Unknown

Environment

Amplitude configuration

Configuration(
        apiKey = apiKey,
        context = context,
        loggerProvider = AmplitudeLoggerProvider(debug),
        trackingSessionEvents = true,
        useAppSetIdForDeviceId = true,
        migrateLegacyData = true
    )
liuyang1520 commented 7 months ago

Hi @niqo01 ,

Thanks for reporting this! I haven't seen this error before, by reading the source code, it was logged here.

Thanks!

niqo01 commented 7 months ago

Hi @liuyang1520

I am not able to provide a more detailed stacktrace. It's happening on a small subset of production devices in the wild. If a new version the Amplitude SDK could return the actual exception in the logger, that would be great.

Nicolas

liuyang1520 commented 7 months ago

Hi @niqo01 ,

Got it. Is it possible for you to try this troubleshooting plugin? By reading the short error message above, it might be helpful to understand the event payload and see whether it is convertible to JSON.

Thanks!

niqo01 commented 5 months ago

Here is a stacktrace:

Non-fatal Exception: com.couchsurfing.mobile.data.api.AmplitudeException: Stack trace: org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject
    at org.json.JSON.typeMismatch(JSON.java:112)
    at org.json.JSONObject.<init>(JSONObject.java:169)
    at org.json.JSONObject.<init>(JSONObject.java:182)
    at com.amplitude.core.utilities.HttpClient$upload$1.close(SourceFile:116)
    at com.amplitude.core.platform.EventPipeline$upload$1.invokeSuspend(SourceFile:231)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(SourceFile:9)
    at kotlinx.coroutines.DispatchedTask.run(SourceFile:113)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)

       at com.couchsurfing.mobile.data.api.AmplitudeException.<init>(AmplitudeException.java:3)
       at com.couchsurfing.mobile.data.api.AmplitudeLoggerProvider$getLogger$1.error(KotlinProvider.kt:50)
       at com.amplitude.core.utilities.LoggerExtensionsKt.logWithStackTrace(LoggerExtensions.kt:10)
       at com.amplitude.core.platform.EventPipeline$upload$1.invokeSuspend(EventPipeline.kt:147)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:923)

My guess is that the amplitude backend returns HTML response sometimes, probably for some 5XX or 4XX issues and when the SDK create a JSONObject for the response, it raises an exception. See the code here: https://github.com/amplitude/Amplitude-Kotlin/blob/main/core/src/main/java/com/amplitude/core/utilities/HttpClient.kt#L42