-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev-launcher][Android] Send uncaught exceptions to bundler server (#…
…15938) # Why Part of ENG-2401. # How Send uncaught exceptions to the bundler server if possible. Our dev server has its endpoint where we can send logs. So I've just used it. # Test Plan - throws an exception in the start method of the app and check if a log occurs in the console.
- Loading branch information
Showing
5 changed files
with
174 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...-dev-launcher/android/src/main/java/expo/modules/devlauncher/logs/DevLauncherRemoteLog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package expo.modules.devlauncher.logs | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.annotations.Expose | ||
|
||
internal interface DevLauncherRemoteLogBody { | ||
val message: String | ||
val stack: String? | ||
|
||
override fun toString(): String | ||
} | ||
|
||
internal class DevLauncherSimpleRemoteLogBody(override val message: String) : DevLauncherRemoteLogBody { | ||
override val stack: String? = null | ||
|
||
override fun toString(): String = message | ||
} | ||
|
||
internal class DevLauncherExceptionRemoteLogBody(exception: Throwable) : DevLauncherRemoteLogBody { | ||
override val message: String = exception.toString() | ||
override val stack: String = exception.stackTraceToRemoteLogString() | ||
|
||
override fun toString(): String = Gson().toJson(this) | ||
} | ||
|
||
@Suppress("UNUSED") | ||
internal data class DevLauncherRemoteLog( | ||
val logBody: DevLauncherRemoteLogBody, | ||
@Expose val level: String = "error" | ||
) { | ||
@Expose | ||
val includesStack = logBody.stack !== null | ||
|
||
@Expose | ||
private val body = logBody.toString() | ||
|
||
fun toJson(): String { | ||
return GsonBuilder() | ||
.excludeFieldsWithoutExposeAnnotation() | ||
.create() | ||
.toJson(this) | ||
} | ||
} | ||
|
||
internal fun Throwable.stackTraceToRemoteLogString(): String { | ||
val baseTrace = stackTrace.joinToString(separator = "\n") { | ||
it.toString() | ||
} | ||
|
||
cause?.let { | ||
return baseTrace + "\nCaused By ${it.stackTraceToRemoteLogString()}" | ||
} | ||
|
||
return baseTrace | ||
} |
49 changes: 49 additions & 0 deletions
49
...uncher/android/src/main/java/expo/modules/devlauncher/logs/DevLauncherRemoteLogManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package expo.modules.devlauncher.logs | ||
|
||
import android.net.Uri | ||
import android.os.Build | ||
import expo.modules.devlauncher.helpers.await | ||
import expo.modules.devlauncher.helpers.post | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.MediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody | ||
|
||
class DevLauncherRemoteLogManager(private val httpClient: OkHttpClient, private val url: Uri) { | ||
private val batch: MutableList<DevLauncherRemoteLog> = mutableListOf() | ||
|
||
fun deferError(throwable: Throwable) { | ||
addToBatch( | ||
DevLauncherRemoteLog( | ||
DevLauncherExceptionRemoteLogBody(throwable) | ||
) | ||
) | ||
} | ||
|
||
fun deferError(message: String) { | ||
addToBatch( | ||
DevLauncherRemoteLog( | ||
DevLauncherSimpleRemoteLogBody(message) | ||
) | ||
) | ||
} | ||
|
||
private fun addToBatch(log: DevLauncherRemoteLog) { | ||
batch.add(log) | ||
} | ||
|
||
fun sendSync() = runBlocking { | ||
val content = batch.joinToString(separator = ",") { it.toJson() } | ||
val requestBody = RequestBody.create(MediaType.get("application/json"), "[$content]") | ||
|
||
val postRequest = post( | ||
url, | ||
requestBody, | ||
"Device-Id" to Build.ID, | ||
"Device-Name" to Build.DISPLAY | ||
) | ||
postRequest.await(httpClient) | ||
|
||
batch.clear() | ||
} | ||
} |