Skip to content

Commit

Permalink
Backend: handle null response from Eth server
Browse files Browse the repository at this point in the history
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 nblockchain#282
  • Loading branch information
webwarrior-ws committed Aug 19, 2024
1 parent 9feb545 commit 13762be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/GWallet.Backend/Ether/EtherExceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion src/GWallet.Backend/Ether/EtherServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/GWallet.Backend/ServerManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ module ServerManager =
let web3Func (web3: Ether.SomeWeb3): Async<decimal> =
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
}

Expand Down

0 comments on commit 13762be

Please sign in to comment.