Skip to content

Commit 5d13d4b

Browse files
committed
Merge remote-tracking branch 'origin/stable'
To bring error handling.
2 parents 8f97232 + b6468e8 commit 5d13d4b

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/GWallet.Backend/Ether/EtherExceptions.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type RpcErrorCode =
3333
| CannotFulfillRequest = -32046
3434
| ResourceNotFound = -32001
3535
| InternalError = -32603
36+
| UnparsableResponseType = -39000
3637

3738
type ServerCannotBeResolvedException =
3839
inherit CommunicationUnsuccessfulException
@@ -95,3 +96,10 @@ type UnhandledWebException =
9596
}
9697
new (info: SerializationInfo, context: StreamingContext) =
9798
{ inherit Exception (info, context) }
99+
100+
/// Exception indicating that response JSON contains null value where it should not.
101+
/// E.g. {"jsonrpc":"2.0","id":1,"result":null}
102+
type AbnormalNullValueInJsonResponseException(message: string) =
103+
inherit CommunicationUnsuccessfulException(message)
104+
105+
static member BalanceJobErrorMessage = "Abnormal null response from balance job"

src/GWallet.Backend/Ether/EtherServer.fs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ module Server =
136136
raise <| ServerTimedOutException(exMsg, httpReqEx)
137137
if HttpRequestExceptionMatchesErrorCode httpReqEx (int CloudFlareError.OriginUnreachable) then
138138
raise <| ServerTimedOutException(exMsg, httpReqEx)
139+
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.RequestTimeout) then
140+
raise <| ServerTimedOutException(exMsg, httpReqEx)
139141

140142
if HttpRequestExceptionMatchesErrorCode httpReqEx (int CloudFlareError.OriginSslHandshakeError) then
141143
raise <| ServerChannelNegotiationException(exMsg, CloudFlareError.OriginSslHandshakeError, httpReqEx)
@@ -163,6 +165,8 @@ module Server =
163165
raise <| ServerUnavailableException(exMsg, httpReqEx)
164166
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.NotFound) then
165167
raise <| ServerUnavailableException(exMsg, httpReqEx)
168+
if HttpRequestExceptionMatchesErrorCode httpReqEx (int HttpStatusCode.Gone) then
169+
raise <| ServerUnavailableException(exMsg, httpReqEx)
166170

167171
// this happened once with ETC (www.ethercluster.com/etc) when trying to broadcast a transaction
168172
// (not sure if it's correct to ignore it but I can't fathom how the tx could be a bad request:
@@ -234,6 +238,8 @@ module Server =
234238
raise <| ServerMisconfiguredException(exMsg, rpcResponseEx)
235239
| i when i = int RpcErrorCode.InternalError ->
236240
raise <| ServerFaultException(exMsg, rpcResponseEx)
241+
| j when j = int RpcErrorCode.UnparsableResponseType ->
242+
raise <| ServerFaultException(exMsg, rpcResponseEx)
237243
| _ ->
238244
raise
239245
<| Exception (SPrintF3 "RpcResponseException with RpcError Code <%i> and Message '%s' (%s)"
@@ -482,7 +488,10 @@ module Server =
482488
let! cancelToken = Async.CancellationToken
483489
let task =
484490
web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address, null, cancelToken)
485-
return! Async.AwaitTask task
491+
let! txCount = Async.AwaitTask task
492+
if isNull txCount then
493+
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from tx count job"
494+
return txCount
486495
}
487496
GetRandomizedFuncs currency web3Func
488497
return! faultTolerantEtherClient.Query
@@ -498,7 +507,7 @@ module Server =
498507
web3.Eth.Blocks.GetBlockNumber.SendRequestAsync (null, cancelToken)
499508
|> Async.AwaitTask
500509
if isNull latestBlock then
501-
failwith "latestBlock somehow is null"
510+
raise <| AbnormalNullValueInJsonResponseException "latestBlock somehow is null"
502511

503512
let blockToCheck = BigInteger.Subtract(latestBlock.Value,
504513
NUMBER_OF_CONFIRMATIONS_TO_CONSIDER_BALANCE_CONFIRMED)
@@ -560,8 +569,10 @@ module Server =
560569
let task = web3.Eth.GetBalance.SendRequestAsync (address, null, cancelToken)
561570
return! Async.AwaitTask task
562571
}
563-
if Object.ReferenceEquals(balance, null) then
564-
failwith "Weird null response from balance job"
572+
if isNull balance then
573+
raise <|
574+
AbnormalNullValueInJsonResponseException
575+
AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage
565576
return UnitConversion.Convert.FromWei(balance.Value, UnitConversion.EthUnit.Ether)
566577
}
567578
GetRandomizedFuncs currency web3Func
@@ -591,7 +602,7 @@ module Server =
591602

592603
let contractHandler = web3.Eth.GetContractHandler contractAddress
593604
if isNull contractHandler then
594-
failwith "contractHandler somehow is null"
605+
raise <| AbnormalNullValueInJsonResponseException "contractHandler somehow is null"
595606

596607
let! cancelToken = Async.CancellationToken
597608
cancelToken.ThrowIfCancellationRequested()
@@ -653,7 +664,10 @@ module Server =
653664
let! cancelToken = Async.CancellationToken
654665
let task =
655666
contractHandler.EstimateGasAsync<TransferFunction>(transferFunctionMsg, cancelToken)
656-
return! Async.AwaitTask task
667+
let! fee = Async.AwaitTask task
668+
if isNull fee then
669+
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from transfer fee job"
670+
return fee
657671
}
658672
GetRandomizedFuncs account.Currency web3Func
659673
return! faultTolerantEtherClient.Query
@@ -676,6 +690,8 @@ module Server =
676690
let! cancelToken = Async.CancellationToken
677691
let task = web3.Eth.GasPrice.SendRequestAsync(null, cancelToken)
678692
let! hexBigInteger = Async.AwaitTask task
693+
if isNull hexBigInteger then
694+
raise <| AbnormalNullValueInJsonResponseException "Abnormal null response from gas price job"
679695
if hexBigInteger.Value = BigInteger 0 then
680696
return failwith "Some server returned zero for gas price, which is invalid"
681697
return hexBigInteger
@@ -706,7 +722,12 @@ module Server =
706722
let! cancelToken = Async.CancellationToken
707723
let task =
708724
web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(transaction, null, cancelToken)
709-
return! Async.AwaitTask task
725+
let! response = Async.AwaitTask task
726+
if isNull response then
727+
raise <|
728+
AbnormalNullValueInJsonResponseException
729+
"Abnormal null response from broadcast transaction job"
730+
return response
710731
}
711732
GetRandomizedFuncs currency web3Func
712733
try
@@ -737,6 +758,10 @@ module Server =
737758
let task =
738759
web3.TransactionManager.TransactionReceiptService.PollForReceiptAsync(txHash, cancelToken)
739760
let! transactionReceipt = Async.AwaitTask task
761+
if isNull transactionReceipt || isNull transactionReceipt.GasUsed || isNull transactionReceipt.Status then
762+
raise <|
763+
AbnormalNullValueInJsonResponseException
764+
(SPrintF1 "Abnormal null response when getting details from tx receipt (%A)" transactionReceipt)
740765
return {
741766
GasUsed = transactionReceipt.GasUsed.Value
742767
Status = transactionReceipt.Status.Value

src/GWallet.Backend/ServerManager.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ module ServerManager =
128128
let web3Func (web3: Ether.SomeWeb3): Async<decimal> =
129129
async {
130130
let! balance = Async.AwaitTask (web3.Eth.GetBalance.SendRequestAsync ETH_GENESISBLOCK_ADDRESS)
131+
if isNull balance then
132+
raise <|
133+
Ether.AbnormalNullValueInJsonResponseException
134+
Ether.AbnormalNullValueInJsonResponseException.BalanceJobErrorMessage
131135
return balance.Value |> decimal
132136
}
133137

0 commit comments

Comments
 (0)