Review all exceptions and classify them as Defects and Errors

This commit is contained in:
Zahary Karadjov 2020-02-05 13:29:54 +01:00 committed by zah
parent 81e0ae12b0
commit d70bb85392
14 changed files with 34 additions and 28 deletions

View File

@ -21,7 +21,7 @@
import nimcrypto/utils, endians
type
UUIDException = object of Exception
UUIDException = object of CatchableError
UUID* = object
## Represents UUID object

View File

@ -13,7 +13,7 @@ type
Success, ## Operation was successful
Error ## Operation failed
EthKeysException* = object of Exception
EthKeysException* = object of CatchableError
## Exception generated by this module
when not defined(native):

View File

@ -50,7 +50,7 @@ type
SignatureNR* = secp256k1_ecdsa_signature
## Representation of non-recoverable signature
Secp256k1Exception* = object of Exception
Secp256k1Exception* = object of CatchableError
## Exceptions generated by `libsecp256k1`
EthKeysContext = ref object

View File

@ -79,7 +79,7 @@ type
egressMac*: keccak256
ingressMac*: keccak256
AuthException* = object of Exception
AuthException* = object of CatchableError
template toa(a, b, c: untyped): untyped =
toOpenArray((a), (b), (b) + (c) - 1)

View File

@ -16,7 +16,7 @@ const
emptyMac* = array[0, byte]([])
type
EciesException* = object of Exception
EciesException* = object of CatchableError
EciesStatus* = enum
Success, ## Operation was successful
BufferOverrun, ## Output buffer size is too small

View File

@ -34,7 +34,7 @@ type
pubkey*: PublicKey ## Node public key
address*: Address ## Node address
ENodeException* = object of Exception
ENodeException* = object of CatchableError
proc raiseENodeError(status: ENodeStatus) =
if status == IncorrectIP:

View File

@ -95,7 +95,7 @@ type
key*: string
value*: Blob
HandshakeError* = object of Exception
HandshakeError* = object of CatchableError
LesTime* = int # this is in milliseconds
BufValueInt* = int

View File

@ -11,8 +11,6 @@ type
pendingLists: seq[tuple[remainingItems, outBytes: int]]
output: Bytes
PrematureFinalizationError* = object of Exception
IntLike* = concept x, y
type T = type(x)
@ -256,9 +254,7 @@ proc initRlpList*(listSize: int): RlpWriter =
# TODO: This should return a lent value
proc finish*(self): Bytes =
if pendingLists.len > 0:
raise newException(PrematureFinalizationError,
"Insufficient number of elements written to a started list")
doAssert pendingLists.len == 0, "Insufficient number of elements written to a started list"
result = output
proc encode*[T](v: T): Bytes =

View File

@ -1,5 +1,5 @@
type
StorageError* = object of Exception
StorageError* = object of CatchableError
template raiseStorageInitError* =
raise newException(StorageError, "failure to initialize storage")

View File

@ -1,6 +1,6 @@
import
sequtils,
stew/ranges/[ptr_arith, bitranges], eth/rlp/types
stew/ranges/[ptr_arith, bitranges], eth/rlp/types, trie_defs
type
TrieNodeKind* = enum
@ -22,8 +22,8 @@ type
of LEAF_TYPE:
value*: BytesRange
InvalidNode* = object of Exception
ValidationError* = object of Exception
InvalidNode* = object of CorruptedTrieDatabase
ValidationError* = object of CorruptedTrieDatabase
# ----------------------------------------------
template sliceToEnd*(r: TrieBitRange, index: int): TrieBitRange =

View File

@ -12,7 +12,7 @@ type
db: DB
rootHash: TrieNodeKey
NodeOverrideError* = object of Exception
NodeOverrideError* = object of CatchableError
let
zeroHash* = zeroBytesRange

View File

@ -4,7 +4,9 @@ import
type
DB = TrieDatabaseRef
InvalidKeyError* = object of Exception
# TODO: replace the usages of this with regular asserts
InvalidKeyError* = object of Defect
template query(db: DB, nodeHash: TrieNodeKey): BytesRange =
db.get(nodeHash.toOpenArray).toRange

View File

@ -19,10 +19,6 @@ type
TrieNode = Rlp
TrieError* = object of Exception
CorruptedTrieError* = object of TrieError
PersistenceFailure* = object of TrieError
template len(key: TrieNodeKey): int =
key.usedBytes.int
@ -133,7 +129,7 @@ proc getAux(db: DB, nodeRlp: Rlp, path: NibblesRange): BytesRange =
let nextLookup = branch.getLookup
return getAux(db, nextLookup, path.slice(1))
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
proc get*(self: HexaryTrie; key: BytesRange): BytesRange =
@ -173,7 +169,7 @@ proc getKeysAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesRange]])
doAssert(path.len mod 2 == 0)
return path.getBytes
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
iterator keys*(self: HexaryTrie): BytesRange =
@ -212,7 +208,7 @@ proc getValuesAux(db: DB, stack: var seq[Rlp]): BytesRange =
if not lastElem.isEmpty:
return lastElem.toBytes
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
iterator values*(self: HexaryTrie): BytesRange =
@ -255,7 +251,7 @@ proc getPairsAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesRange]]
doAssert(path.len mod 2 == 0)
return (path.getBytes, lastElem.toBytes)
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
iterator pairs*(self: HexaryTrie): (BytesRange, BytesRange) =
@ -311,7 +307,7 @@ iterator replicate*(self: HexaryTrie): (BytesRange, BytesRange) =
key.replaceLastNibble(i.byte)
pushOrYield(branch)
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
proc getValues*(self: HexaryTrie): seq[BytesRange] =
@ -350,7 +346,7 @@ proc getBranchAux(db: DB, node: BytesRange, path: NibblesRange, output: var seq[
output.add nextLookup
getBranchAux(db, nextLookup, path.slice(1), output)
else:
raise newException(CorruptedTrieError,
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")
proc getBranch*(self: HexaryTrie; key: BytesRange): seq[BytesRange] =

View File

@ -8,6 +8,18 @@ type
KeccakHash* = MDigest[256]
BytesContainer* = ByteRange | Bytes | string
TrieError* = object of CatchableError
# A common base type of all Trie errors.
PersistenceFailure* = object of TrieError
# The backing database of the trie was not able to carry out
# the storage or retrieval request.
CorruptedTrieDatabase* = object of Defect
# We consider this a Defect, because the software cannot safely
# operate if its database has been tampered with. A swift crash
# will be a more appropriate response.
# can't be a const: https://github.com/status-im/nim-eth/issues/6
# we can't initialise it here, but since it's already zeroed memory, we don't need to
var zeroBytesRange* {.threadvar.}: ByteRange