fix: capture RpcExceptions generated by stickers that would not allow login

This commit is contained in:
Richard Ramos 2020-07-16 11:40:39 -04:00 committed by Iuri Matias
parent 74a38c671d
commit 4fae0eb100
3 changed files with 42 additions and 27 deletions

View File

@ -9,6 +9,9 @@ import ../signals/types as signal_types
import ens
import eth/common/eth_types
logScope:
topics = "chat-model"
type
ChatUpdateArgs* = ref object of Args
chats*: seq[Chat]
@ -99,12 +102,16 @@ proc getPurchasedStickerPacks*(self: ChatModel, address: EthAddress): seq[int] =
if self.purchasedStickerPacks != @[]:
return self.purchasedStickerPacks
try:
var balance = status_stickers.getBalance(address)
# balance = 2 # hardcode to test support of purchased sticker packs, because buying sticker packs is proving very difficult on testnet
var tokenIds = toSeq[0..<balance].map(idx => status_stickers.tokenOfOwnerByIndex(address, idx))
# tokenIds = @[1, 2] # hardcode to test support of purchased sticker packs
self.purchasedStickerPacks = tokenIds.map(tokenId => status_stickers.getPackIdFromTokenId(tokenId))
result = self.purchasedStickerPacks
except RpcException:
error "Error in getPurchasedStickerPacks", message = getCurrentExceptionMsg()
result = @[]
proc getInstalledStickerPacks*(self: ChatModel): Table[int, StickerPack] =
if self.installedStickerPacks != initTable[int, StickerPack]():
@ -117,6 +124,7 @@ proc getAvailableStickerPacks*(self: ChatModel): Table[int, StickerPack] =
if self.availableStickerPacks != initTable[int, StickerPack]():
return self.availableStickerPacks
try:
let numPacks = status_stickers.getPackCount()
for i in 0..<numPacks:
try:
@ -125,6 +133,9 @@ proc getAvailableStickerPacks*(self: ChatModel): Table[int, StickerPack] =
except:
continue
result = self.availableStickerPacks
except RpcException:
error "Error in getAvailableStickerPacks", message = getCurrentExceptionMsg()
result = initTable[int, StickerPack]()
proc getRecentStickers*(self: ChatModel): seq[Sticker] =
result = status_stickers.getRecentStickers()

View File

@ -65,8 +65,8 @@ proc getBalance*(address: EthAddress): int =
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting stickers balance: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting stickers balance: " & response.error.message)
if response.result == "0x":
return 0
result = fromHex[int](response.result)
@ -81,8 +81,8 @@ proc getPackCount*(): int =
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting stickers balance: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting stickers balance: " & response.error.message)
if response.result == "0x":
return 0
result = fromHex[int](response.result)
@ -97,8 +97,8 @@ proc getPackData*(id: int): StickerPack =
}, "latest"]
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting sticker pack data: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting sticker pack data: " & response.error.message)
let packData = contracts.decodeContractResponse[PackData](response.result)
@ -136,8 +136,8 @@ proc buyPack*(packId: int, address: EthAddress, price: Stuint[256], password: st
let responseStr = status.sendTransaction($payload, password)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting stickers balance: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting stickers balance: " & response.error.message)
result = response.result # should be a tx receipt
proc tokenOfOwnerByIndex*(address: EthAddress, idx: int): int =
@ -149,8 +149,8 @@ proc tokenOfOwnerByIndex*(address: EthAddress, idx: int): int =
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting owned tokens: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting owned tokens: " & response.error.message)
if response.result == "0x":
return 0
result = fromHex[int](response.result)
@ -164,8 +164,8 @@ proc getPackIdFromTokenId*(tokenId: int): int =
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if response.error != "":
raise newException(RpcException, "Error getting pack id from token id: " & response.error)
if not response.error.isNil:
raise newException(RpcException, "Error getting pack id from token id: " & response.error.message)
if response.result == "0x":
return 0
result = fromHex[int](response.result)

View File

@ -57,12 +57,16 @@ type
keyUid*: string
photoPath*: string
type RpcError* = ref object
code*: int
message*: string
type
RpcResponse* = ref object
jsonrpc*: string
result*: string
id*: int
error*: string
error*: RpcError
proc toAccount*(account: GeneratedAccount): Account =
result = Account(name: account.name, photoPath: account.photoPath, keyUid: account.address)