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

Ensure all Json.parseToJsonElement calls are wrapped in try-catch wit… #399

Merged
merged 2 commits into from
Jun 7, 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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.7.60"
version = "1.7.61"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import exchange.dydx.abacus.responses.StateResponse
import exchange.dydx.abacus.state.changes.Changes
import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.state.manager.BlockAndTime
import exchange.dydx.abacus.utils.Logger
import kollections.iListOf
import kollections.toIList
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json

internal fun TradingStateMachine.account(payload: String): StateChanges {
val json = parser.asMap(Json.parseToJsonElement(payload))
val json = try {
parser.asMap(Json.parseToJsonElement(payload))
} catch (exception: SerializationException) {
Logger.e {
"Failed to deserialize account: $payload \n" +
"Exception: $exception"
}
null
}
return if (json != null) {
receivedAccount(json)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ package exchange.dydx.abacus.state.model

import exchange.dydx.abacus.state.changes.Changes
import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.utils.Logger
import kollections.iEmptyList
import kollections.iListOf
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonArray

internal fun TradingStateMachine.feeDiscounts(payload: String): StateChanges {
val json = Json.parseToJsonElement(payload).jsonArray.toList()
val json = try {
Json.parseToJsonElement(payload).jsonArray.toList()
} catch (exception: SerializationException) {
Logger.e {
"Failed to deserialize feeDiscounts: $payload \n" +
"Exception: $exception"
}
return StateChanges(iEmptyList())
}
return receivedFeeDiscounts(json)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package exchange.dydx.abacus.state.model

import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.utils.Logger
import kollections.iListOf
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject

Expand All @@ -17,8 +19,16 @@ internal fun TradingStateMachine.onChainRewardsParams(payload: String): StateCha
return StateChanges(iListOf())
}

internal fun TradingStateMachine.onChainRewardTokenPrice(payload: String): StateChanges {
val json = Json.parseToJsonElement(payload).jsonObject.toMap()
internal fun TradingStateMachine.onChainRewardTokenPrice(payload: String): StateChanges? {
val json = try {
Json.parseToJsonElement(payload).jsonObject.toMap()
} catch (exception: SerializationException) {
Logger.e {
"Failed to deserialize onChainRewardTokenPrice: $payload \n" +
"Exception: $exception"
}
null
}
val map = parser.asMap(json)
val price = parser.asMap(map?.get("marketPrice"))
rewardsParams =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package exchange.dydx.abacus.state.model

import exchange.dydx.abacus.state.changes.Changes
import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.utils.Logger
import kollections.iEmptyList
import kollections.iListOf
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject

Expand Down Expand Up @@ -73,8 +76,13 @@ internal fun TradingStateMachine.squidRouteV2(
internal fun TradingStateMachine.squidStatus(
payload: String,
transactionId: String?
): StateChanges? {
val json = Json.parseToJsonElement(payload).jsonObject.toMap()
): StateChanges {
val json = try {
Json.parseToJsonElement(payload).jsonObject.toMap()
} catch (exception: SerializationException) {
Logger.e { "Failed to deserialize squidStatus: $payload \nException: $exception" }
return StateChanges(iEmptyList())
}
transferStatuses = routerProcessor.receivedStatus(transferStatuses, json, transactionId)
return StateChanges(iListOf(Changes.transferStatuses))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import exchange.dydx.abacus.responses.SocketInfo
import exchange.dydx.abacus.state.changes.Changes
import exchange.dydx.abacus.state.changes.StateChanges
import exchange.dydx.abacus.state.manager.BlockAndTime
import exchange.dydx.abacus.utils.Logger
import kollections.iListOf
import kollections.iMutableListOf
import kollections.toIList
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonArray

Expand Down Expand Up @@ -226,7 +228,11 @@ internal fun TradingStateMachine.onChainAccountBalances(payload: String): StateC
val account = json.jsonArray.toList()
this.wallet = walletProcessor.receivedAccountBalances(wallet, account)
return StateChanges(iListOf(Changes.accountBalances), null)
} catch (e: Exception) {
} catch (exception: SerializationException) {
Logger.e {
"Failed to deserialize onChainAccountBalances: $payload \n" +
"Exception: $exception"
}
StateChanges(iListOf())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import kollections.iMutableListOf
import kollections.iMutableMapOf
import kollections.toIList
import kollections.toIMutableMap
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlin.math.max
Expand Down Expand Up @@ -246,7 +247,7 @@ open class TradingStateMachine(
val json =
try {
Json.parseToJsonElement(jsonString).jsonObject.toMap()
} catch (e: Exception) {
} catch (e: SerializationException) {
errors.add(
ParsingError(
ParsingErrorType.ParsingError,
Expand Down
7 changes: 3 additions & 4 deletions src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kollections.toIList
import kollections.toIMap
import kollections.toIMutableList
import kotlinx.datetime.Instant
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonNull
Expand Down Expand Up @@ -373,9 +374,8 @@ class Parser : ParserProtocol {
if (text == null) return null
val map = try {
Json.parseToJsonElement(text).jsonObject.toMap().toIMap()
} catch (e: Exception) {
} catch (e: SerializationException) {
Logger.e { "Unable to decode json object: $text" }
Logger.e { "Exception: $e" }
null
}
return map
Expand All @@ -385,9 +385,8 @@ class Parser : ParserProtocol {
if (text == null) return null
val list = try {
Json.parseToJsonElement(text).jsonArray.toIList()
} catch (e: Exception) {
} catch (e: SerializationException) {
Logger.e { "Unable to decode json object: $text" }
Logger.e { "Exception: $e" }
null
}
return list
Expand Down
Loading