From 13762be958b8b007e858dea8679f1e5cf3702e79 Mon Sep 17 00:00:00 2001 From: webwarrior Date: Mon, 12 Aug 2024 11:12:53 +0200 Subject: [PATCH] Backend: handle null response from Eth server Introduce WeirdNullResponseException type and raise it when response for balance request for Ethereum returns json with "result" field value of `null`. This way server will be marked as faulty instead of crashing the application. Fixes https://github.com/nblockchain/geewallet/issues/282 --- src/GWallet.Backend/Ether/EtherExceptions.fs | 7 +++++++ src/GWallet.Backend/Ether/EtherServer.fs | 4 +++- src/GWallet.Backend/ServerManager.fs | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/GWallet.Backend/Ether/EtherExceptions.fs b/src/GWallet.Backend/Ether/EtherExceptions.fs index a08d73033..11c6c3fc1 100644 --- a/src/GWallet.Backend/Ether/EtherExceptions.fs +++ b/src/GWallet.Backend/Ether/EtherExceptions.fs @@ -95,3 +95,10 @@ type UnhandledWebException = } new (info: SerializationInfo, context: StreamingContext) = { inherit Exception (info, context) } + +/// Exception indicating that response JSON contains null value where it should not. +/// E.g. {"jsonrpc":"2.0","id":1,"result":null} +type AbnormalNullValueInJsonResponseException(message: string) = + inherit CommunicationUnsuccessfulException(message) + + static member BalanceJobErrorMessage = "Abnormal null response from balance job" diff --git a/src/GWallet.Backend/Ether/EtherServer.fs b/src/GWallet.Backend/Ether/EtherServer.fs index 3e86bbb9f..acf2b915c 100644 --- a/src/GWallet.Backend/Ether/EtherServer.fs +++ b/src/GWallet.Backend/Ether/EtherServer.fs @@ -561,7 +561,9 @@ module Server = return! Async.AwaitTask task } if Object.ReferenceEquals(balance, null) then - failwith "Weird null response from balance job" + raise <| + AbnormalNullValueInJsonResponseException + AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage return UnitConversion.Convert.FromWei(balance.Value, UnitConversion.EthUnit.Ether) } GetRandomizedFuncs currency web3Func diff --git a/src/GWallet.Backend/ServerManager.fs b/src/GWallet.Backend/ServerManager.fs index 958ff3d6e..8dd56ef9e 100644 --- a/src/GWallet.Backend/ServerManager.fs +++ b/src/GWallet.Backend/ServerManager.fs @@ -128,6 +128,10 @@ module ServerManager = let web3Func (web3: Ether.SomeWeb3): Async = async { let! balance = Async.AwaitTask (web3.Eth.GetBalance.SendRequestAsync ETH_GENESISBLOCK_ADDRESS) + if isNull balance then + raise <| + Ether.AbnormalNullValueInJsonResponseException + Ether.AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage return balance.Value |> decimal }