scoop:scoop.http.asyncio:0.1.0
src/websocket/failure.scoop
Selected production source · src/websocket/failure.scoop
1
package scoop.http.asyncio3
internal fun recordWsOperationFailure(state: WsState, error: Throwable) {4
val normalEnd = error is WebSocketSessionException &&5
(error.kind == WebSocketFailureKind.Closed || error.kind == WebSocketFailureKind.InterruptedMessage)6
if (!state.closing) {7
if (present(state.shutdown) && normalEnd) return8
if (error is ResourceUseException) {9
abortWs(state, Some(error.primaryFailure))10
state.closeFailure = appendWsFailure(state.closeFailure, error.cleanupFailure)11
} else abortWs(state, Some(error))12
} else {13
val cleanup = wsNewCleanup(state.failure, state.closeFailure, error)14
if (present(cleanup)) state.closeFailure = appendWsFailure(state.closeFailure, cleanup!!)15
}16
}18
internal fun appendWsFailure(previous: Throwable?, error: Throwable): Throwable? =19
if (!present(previous)) Some(error)20
else if (wsContainsFailure(previous!!, error)) previous21
else Some(ResourceUseException(previous!!, error))23
private fun wsNewCleanup(primary: Throwable?, previous: Throwable?, error: Throwable): Throwable? {24
if ((present(primary) && wsContainsFailure(primary!!, error)) ||25
(present(previous) && wsContainsFailure(previous!!, error))) return None26
if (error is CancellationException) return None27
if (error is WebSocketSessionException &&28
(error.kind == WebSocketFailureKind.Closed || error.kind == WebSocketFailureKind.InterruptedMessage)) return None29
if (error is ResourceUseException) {30
val first = wsNewCleanup(primary, previous, error.primaryFailure)31
val second = wsNewCleanup(primary, if (present(first)) appendWsFailure(previous, first!!) else previous,32
error.cleanupFailure)33
if (!present(first)) return second34
if (!present(second)) return first35
if (first!! === error.primaryFailure && second!! === error.cleanupFailure) return Some(error)36
return Some(ResourceUseException(first!!, second!!))37
}38
return Some(error)39
}41
private fun wsContainsFailure(root: Throwable, target: Throwable): Boolean {42
if (root === target) return true43
if (root is ResourceUseException)44
return wsContainsFailure(root.primaryFailure, target) || wsContainsFailure(root.cleanupFailure, target)45
return false46
}