Scoop API

scoop:scoop.http.asyncio:0.1.0

src/websocket/failure.scoop

Selected production source · src/websocket/failure.scoop

1package scoop.http.asyncio
3internal 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) return
8 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 }
18internal fun appendWsFailure(previous: Throwable?, error: Throwable): Throwable? =
19 if (!present(previous)) Some(error)
20 else if (wsContainsFailure(previous!!, error)) previous
21 else Some(ResourceUseException(previous!!, error))
23private fun wsNewCleanup(primary: Throwable?, previous: Throwable?, error: Throwable): Throwable? {
24 if ((present(primary) && wsContainsFailure(primary!!, error)) ||
25 (present(previous) && wsContainsFailure(previous!!, error))) return None
26 if (error is CancellationException) return None
27 if (error is WebSocketSessionException &&
28 (error.kind == WebSocketFailureKind.Closed || error.kind == WebSocketFailureKind.InterruptedMessage)) return None
29 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 second
34 if (!present(second)) return first
35 if (first!! === error.primaryFailure && second!! === error.cleanupFailure) return Some(error)
36 return Some(ResourceUseException(first!!, second!!))
37 }
38 return Some(error)
41private fun wsContainsFailure(root: Throwable, target: Throwable): Boolean {
42 if (root === target) return true
43 if (root is ResourceUseException)
44 return wsContainsFailure(root.primaryFailure, target) || wsContainsFailure(root.cleanupFailure, target)
45 return false