Fix WebSocket error detection logic
This commit is contained in:
parent
ce8ac8d305
commit
a72fd9206b
2 changed files with 10 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ package io.heckel.ntfy.service
|
|||
|
||||
import okhttp3.internal.http2.StreamResetException
|
||||
import java.io.EOFException
|
||||
import java.net.ProtocolException
|
||||
|
||||
interface Connection {
|
||||
fun start()
|
||||
|
|
@ -57,6 +58,13 @@ fun isConnectionBrokenException(t: Throwable): Boolean {
|
|||
return t.hasCause<EOFException>() || t.hasCause<StreamResetException>()
|
||||
}
|
||||
|
||||
/**
|
||||
* ProtocolException is thrown by the OkHttp library when the WebSocket handshake fails.
|
||||
*/
|
||||
fun isProtocolException(t: Throwable): Boolean {
|
||||
return t.hasCause<ProtocolException>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the throwable or any of its causes is of the specified type.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import okhttp3.OkHttpClient
|
|||
import okhttp3.Response
|
||||
import okhttp3.WebSocket
|
||||
import okhttp3.WebSocketListener
|
||||
import java.net.ProtocolException
|
||||
import java.util.Calendar
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
|
@ -191,8 +192,8 @@ class WsConnection(
|
|||
// - Handle servers that do not support WebSockets
|
||||
val error = when {
|
||||
isConnectionBrokenException(t) -> null
|
||||
isProtocolException(t) -> WebSocketNotSupportedException(response!!.code, response.message, t)
|
||||
isResponseCode(response, 401, 403) -> NotAuthorizedException(response!!.code, response.message, t)
|
||||
isResponseCode(response, 101) -> WebSocketNotSupportedException(response!!.code, response.message, t)
|
||||
else -> t
|
||||
}
|
||||
connectionDetailsListener(baseUrl, ConnectionState.CONNECTING, error, nextRetryTime)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue