From 2893210976eb843d4aefaa4c4ca26bf34784bf96 Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 14 Mar 2024 08:33:58 -0400 Subject: [PATCH] Handle exception with invalid URLs --- .../protocolImplementations/AbacusRestImp.kt | 9 ++- .../AbacusWebSocketImp.kt | 70 ++++++++++++------- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusRestImp.kt b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusRestImp.kt index 1fa4f61c..dcf651f7 100644 --- a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusRestImp.kt +++ b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusRestImp.kt @@ -67,7 +67,14 @@ class AbacusRestImp : RestProtocol { callback: (String?, Int) -> Unit, ) { var requestBuilder = Request.Builder() - .url(url) + + try { + requestBuilder.url(url) + } catch (e: Exception) { + Log.e(TAG, "AbacusRestImp Invalid URL $url, ${e.message}") + callback(null, 0) + return + } val headers = headers?.toTypedArray() headers?.forEach { (key, value) -> diff --git a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusWebSocketImp.kt b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusWebSocketImp.kt index ebbbee1e..bc843443 100644 --- a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusWebSocketImp.kt +++ b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusWebSocketImp.kt @@ -1,5 +1,6 @@ package exchange.dydx.dydxstatemanager.protocolImplementations +import android.util.Log import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response @@ -10,6 +11,8 @@ import java.util.concurrent.TimeUnit class AbacusWebSocketImp : exchange.dydx.abacus.protocols.WebSocketProtocol { + private val TAG = "AbacusRestImp" + private var url: String? = null private var connected: ((Boolean) -> Unit)? = null private var received: ((String) -> Unit)? = null @@ -40,38 +43,53 @@ class AbacusWebSocketImp : exchange.dydx.abacus.protocols.WebSocketProtocol { private fun connect() { url?.let { url -> - val request = Request.Builder().url(url).build() - val okHttpClient = OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(10, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .build() - - val listener = object : WebSocketListener() { - override fun onOpen(webSocket: WebSocket, response: Response) { - this@AbacusWebSocketImp.webSocket = webSocket - connected?.invoke(true) - } + val request: Request? - override fun onMessage(webSocket: WebSocket, text: String) { - received?.invoke(text) - } + try { + request = Request.Builder().url(url).build() + } catch (e: Exception) { + connected?.invoke(false) + Log.e(TAG, "AbacusWebSocketImp Invalid URL $url, ${e.message}") + return + } - override fun onMessage(webSocket: WebSocket, bytes: ByteString) { - // Handle binary messages if needed - } + if (request != null) { + val okHttpClient = OkHttpClient.Builder() + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .build() - override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { - webSocket.close(NORMAL_CLOSURE, null) - connected?.invoke(false) - } + val listener = object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + this@AbacusWebSocketImp.webSocket = webSocket + connected?.invoke(true) + } - override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - connected?.invoke(false) + override fun onMessage(webSocket: WebSocket, text: String) { + received?.invoke(text) + } + + override fun onMessage(webSocket: WebSocket, bytes: ByteString) { + // Handle binary messages if needed + } + + override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { + webSocket.close(NORMAL_CLOSURE, null) + connected?.invoke(false) + } + + override fun onFailure( + webSocket: WebSocket, + t: Throwable, + response: Response? + ) { + connected?.invoke(false) + } } - } - okHttpClient.newWebSocket(request, listener) + okHttpClient.newWebSocket(request, listener) + } } } }