Skip to content

Commit

Permalink
Feat/rpc-error-data
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamol committed Feb 7, 2024
1 parent 6c52f05 commit efe4d2b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions rpc/src/main/kotlin/acurast/rpc/Rpc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ internal object JsonRpc {
const val RESULT = "result"
const val ERROR = "error"
const val MESSAGE = "message"
const val DATA = "data"
}
}
7 changes: 6 additions & 1 deletion rpc/src/main/kotlin/acurast/rpc/pallet/PalletRpc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acurast.rpc.pallet
import acurast.rpc.JsonRpc
import acurast.rpc.Rpc
import acurast.rpc.engine.RpcEngine
import acurast.rpc.utils.nullableOptString
import com.google.gson.GsonBuilder
import org.json.JSONArray
import org.json.JSONObject
Expand All @@ -14,7 +15,11 @@ public abstract class PalletRpc(override val defaultEngine: RpcEngine) : Rpc {

protected fun handleError(json: JSONObject): Exception =
json.optJSONObject(JsonRpc.Key.ERROR)?.let { error ->
Exception(error.optString(JsonRpc.Key.MESSAGE))
val message = error.optString(JsonRpc.Key.MESSAGE)
val data = error.nullableOptString(JsonRpc.Key.DATA)
val exceptionMessage = data?.let { "$message ($it)" } ?: message

Exception(exceptionMessage)
} ?: Exception("something went wrong")

protected inline fun <reified R> parseJSON(json: String): R =
Expand Down

0 comments on commit efe4d2b

Please sign in to comment.