Redefine result[] error object as ref (see issue #1796) (#1797)

details:
 This does not affect the legacy interface which is the production one,
 currently.
This commit is contained in:
Jordan Hrycaj 2023-10-02 19:05:17 +01:00 committed by GitHub
parent 492543a007
commit ea2b5b4c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 15 deletions

View File

@ -21,7 +21,7 @@ import
export export
CoreDbBackendRef, CoreDbBackendRef,
CoreDbCaptFlags, CoreDbCaptFlags,
CoreDbError, CoreDbErrorRef,
CoreDbKvtBackendRef, CoreDbKvtBackendRef,
CoreDbMptBackendRef, CoreDbMptBackendRef,
CoreDbRef, CoreDbRef,
@ -74,7 +74,7 @@ type
CoreDxChldRef* = CoreDxKvtRef | CoreDxTrieRef | CoreDxTxRef | CoreDxTxID | CoreDxChldRef* = CoreDxKvtRef | CoreDxTrieRef | CoreDxTxRef | CoreDxTxID |
CoreDxCaptRef | CoreDxCaptRef |
CoreDbError | CoreDbErrorRef |
CoreDbBackendRef | CoreDbKvtBackendRef | CoreDbMptBackendRef CoreDbBackendRef | CoreDbKvtBackendRef | CoreDbMptBackendRef
## Shortcut, all modules with a `parent` ## Shortcut, all modules with a `parent`
@ -194,7 +194,7 @@ proc backend*(db: CoreDbRef): CoreDbBackendRef =
result = db.methods.backendFn() result = db.methods.backendFn()
result.parent = db result.parent = db
proc `$$`*(e: CoreDbError): string = proc `$$`*(e: CoreDbErrorRef): string =
## Pretty print error symbol, note that this directive may have side effects ## Pretty print error symbol, note that this directive may have side effects
## as it calls a backend function. ## as it calls a backend function.
e.parent.methods.errorPrintFn(e) e.parent.methods.errorPrintFn(e)

View File

@ -31,7 +31,7 @@ const
CoreDbPersistentTypes* = {LegacyDbPersistent} CoreDbPersistentTypes* = {LegacyDbPersistent}
type type
CoreDbRc*[T] = Result[T,CoreDbError] CoreDbRc*[T] = Result[T,CoreDbErrorRef]
CoreDbCaptFlags* {.pure.} = enum CoreDbCaptFlags* {.pure.} = enum
PersistPut PersistPut
@ -68,7 +68,7 @@ type
# Sub-descriptor: Misc methods for main descriptor # Sub-descriptor: Misc methods for main descriptor
# -------------------------------------------------- # --------------------------------------------------
CoreDbBackendFn* = proc(): CoreDbBackendRef {.noRaise.} CoreDbBackendFn* = proc(): CoreDbBackendRef {.noRaise.}
CoreDbErrorPrintFn* = proc(e: CoreDbError): string {.noRaise.} CoreDbErrorPrintFn* = proc(e: CoreDbErrorRef): string {.noRaise.}
CoreDbInitLegaSetupFn* = proc() {.noRaise.} CoreDbInitLegaSetupFn* = proc() {.noRaise.}
CoreDbMiscFns* = object CoreDbMiscFns* = object
@ -172,7 +172,7 @@ type
new*: CoreDbConstructorFns new*: CoreDbConstructorFns
methods*: CoreDbMiscFns methods*: CoreDbMiscFns
CoreDbError* = object of RootObj CoreDbErrorRef* = ref object of RootRef
## Generic error object ## Generic error object
parent*: CoreDbRef parent*: CoreDbRef

View File

@ -59,7 +59,8 @@ proc validateConstructors(new: CoreDbConstructorFns) =
# ------------ # ------------
proc validateMethodsDesc(e: CoreDbError) = proc validateMethodsDesc(e: CoreDbErrorRef) =
doAssert not e.isNil
doAssert not e.parent.isNil doAssert not e.parent.isNil
proc validateMethodsDesc(eph: EphemMethodsDesc) = proc validateMethodsDesc(eph: EphemMethodsDesc) =
@ -111,7 +112,7 @@ proc validateMethodsDesc(db: CoreDbRef) =
# Public debugging helpers # Public debugging helpers
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc validate*(desc: MethodsDesc | EphemMethodsDesc | CoreDbError) = proc validate*(desc: MethodsDesc | EphemMethodsDesc | CoreDbErrorRef) =
desc.validateMethodsDesc desc.validateMethodsDesc
proc validate*(db: CoreDbRef) = proc validate*(db: CoreDbRef) =

View File

@ -42,7 +42,7 @@ type
LegacyCoreDbMptBE = ref object of CoreDbMptBackendRef LegacyCoreDbMptBE = ref object of CoreDbMptBackendRef
mpt: HexaryTrie mpt: HexaryTrie
LegacyCoreDbError = object of CoreDbError LegacyCoreDbError = ref object of CoreDbErrorRef
ctx: string ## Context where the exception or error occured ctx: string ## Context where the exception or error occured
name: string ## name of exception name: string ## name of exception
msg: string ## Exception info msg: string ## Exception info
@ -124,11 +124,13 @@ proc miscMethods(db: LegacyDbRef): CoreDbMiscFns =
backendFn: proc(): CoreDbBackendRef = backendFn: proc(): CoreDbBackendRef =
db.bless(LegacyCoreDbBE(base: db)), db.bless(LegacyCoreDbBE(base: db)),
errorPrintFn: proc(e: CoreDbError): string = errorPrintFn: proc(e: CoreDbErrorRef): string =
let e = e.LegacyCoreDbError if not e.isNil:
result &= "ctx=\"" & $e.ctx & "\"" & " , " let e = e.LegacyCoreDbError
result &= "name=\"" & $e.name & "\"" & ", " result &= "ctx=\"" & $e.ctx & "\"" & " , "
result &= "msg=\"" & $e.msg & "\"", result &= "name=\"" & $e.name & "\"" & ", "
result &= "msg=\"" & $e.msg & "\""
discard,
legacySetupFn: proc() = legacySetupFn: proc() =
db.tdb.put(EMPTY_ROOT_HASH.data, @[0x80u8])) db.tdb.put(EMPTY_ROOT_HASH.data, @[0x80u8]))

View File

@ -21,7 +21,7 @@ export
# Not all symbols from the object sources will be exported by default # Not all symbols from the object sources will be exported by default
CoreDbCaptFlags, CoreDbCaptFlags,
CoreDbError, CoreDbErrorRef,
CoreDbCaptRef, CoreDbCaptRef,
CoreDbKvtRef, CoreDbKvtRef,
CoreDbMptRef, CoreDbMptRef,