Skip to content

Commit

Permalink
Throw when target crashed event is received
Browse files Browse the repository at this point in the history
Resolves:
#222
  • Loading branch information
joffrey-bion committed Mar 7, 2023
1 parent 4e9809a commit 06e3189
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
7 changes: 7 additions & 0 deletions api/chrome-devtools-kotlin.api
Original file line number Diff line number Diff line change
Expand Up @@ -44973,6 +44973,13 @@ public final class org/hildan/chrome/devtools/protocol/RequestFrame$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class org/hildan/chrome/devtools/protocol/TargetCrashedException : java/lang/Exception {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)V
public final fun getCrashEventName ()Ljava/lang/String;
public final fun getCrashEventPayload ()Lkotlinx/serialization/json/JsonElement;
public final fun getSessionId ()Ljava/lang/String;
}

public abstract class org/hildan/chrome/devtools/targets/AbstractTargetSession {
public synthetic fun <init> (Lorg/hildan/chrome/devtools/protocol/ChromeDPSession;Lorg/hildan/chrome/devtools/targets/SimpleTarget;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Lorg/hildan/chrome/devtools/protocol/ChromeDPSession;Lorg/hildan/chrome/devtools/targets/SimpleTarget;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.hildan.chrome.devtools.protocol

import kotlinx.coroutines.flow.filter
import kotlinx.serialization.json.JsonElement
import kotlinx.coroutines.flow.*
import kotlinx.serialization.json.*
import org.hildan.chrome.devtools.domains.inspector.events.*
import org.hildan.chrome.devtools.domains.target.events.*
import org.hildan.chrome.devtools.domains.target.SessionID
import java.util.concurrent.atomic.AtomicLong

Expand Down Expand Up @@ -45,7 +47,15 @@ internal class ChromeDPSession(
/**
* Subscribes to all events tied to this session.
*/
fun events() = connection.events().filter { it.sessionId == sessionId }
fun events() = connection.events()
.filter { it.sessionId == sessionId }
.onEach {
// We throw to immediately stop collectors when a target will not respond (instead of hanging).
// Note that Inspector.targetCrashed events are received even without InspectorDomain.enable() call.
if (it.eventName in crashEventNames) {
throw TargetCrashedException(it.sessionId, it.eventName, it.payload)
}
}

/**
* Closes the underlying web socket connection, effectively closing every session based on the same web socket
Expand All @@ -55,3 +65,40 @@ internal class ChromeDPSession(
connection.close()
}
}

private val crashEventNames = setOf("Inspector.targetCrashed", "Target.targetCrashed")

/**
* An exception thrown when an [InspectorEvent.TargetCrashed] or [TargetEvent.TargetCrashed] is received.
*/
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
class TargetCrashedException(
/**
* The session ID of the target that crashed, or null if it is the root browser target.
*/
val sessionId: SessionID?,
/**
* The name of the event that triggered this exception.
*/
val crashEventName: String,
/**
* The payload of the crash event that triggered this exception
*/
val crashEventPayload: JsonElement,
) : Exception(buildTargetCrashedMessage(sessionId, crashEventName, crashEventPayload))

private fun buildTargetCrashedMessage(sessionId: SessionID?, crashEventName: String, payload: JsonElement): String {
val payloadText = when (payload) {
is JsonNull -> null
is JsonPrimitive -> if (payload.isString) "\"${payload.content}\"" else payload.content
is JsonObject -> if (payload.size > 0) payload.toString() else null
is JsonArray -> if (payload.size > 0) payload.toString() else null
}
val payloadInfo = if (payloadText == null) "without payload." else "with payload: $payloadText"
val eventInfo = "Received event '$crashEventName' $payloadInfo"
return if (sessionId == null) {
"The browser target has crashed. $eventInfo"
} else {
"The target with session ID $sessionId has crashed. $eventInfo"
}
}

0 comments on commit 06e3189

Please sign in to comment.