Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exception with invalid URLs #26

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.dydxstatemanager.protocolImplementations

import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Loading