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 #282
  • Loading branch information
webwarrior-ws committed Aug 12, 2024
1 parent 9feb545 commit 7f60c88
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/GWallet.Backend/Ether/EtherExceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ type UnhandledWebException =
}
new (info: SerializationInfo, context: StreamingContext) =
{ inherit Exception (info, context) }

/// Exception indicating that response JSON contains null value where it should not
type WeirdNullResponseException(message: string) =
inherit CommunicationUnsuccessfulException(message)
2 changes: 1 addition & 1 deletion src/GWallet.Backend/Ether/EtherServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ module Server =
return! Async.AwaitTask task
}
if Object.ReferenceEquals(balance, null) then
failwith "Weird null response from balance job"
raise <| WeirdNullResponseException "Weird null response from balance job"
return UnitConversion.Convert.FromWei(balance.Value, UnitConversion.EthUnit.Ether)
}
GetRandomizedFuncs currency web3Func
Expand Down
2 changes: 2 additions & 0 deletions src/GWallet.Backend/ServerManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ 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.WeirdNullResponseException "Weird null response from balance job"
return balance.Value |> decimal
}

Expand Down

0 comments on commit 7f60c88

Please sign in to comment.