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

View File

@ -31,7 +31,7 @@ const
CoreDbPersistentTypes* = {LegacyDbPersistent}
type
CoreDbRc*[T] = Result[T,CoreDbError]
CoreDbRc*[T] = Result[T,CoreDbErrorRef]
CoreDbCaptFlags* {.pure.} = enum
PersistPut
@ -68,7 +68,7 @@ type
# Sub-descriptor: Misc methods for main descriptor
# --------------------------------------------------
CoreDbBackendFn* = proc(): CoreDbBackendRef {.noRaise.}
CoreDbErrorPrintFn* = proc(e: CoreDbError): string {.noRaise.}
CoreDbErrorPrintFn* = proc(e: CoreDbErrorRef): string {.noRaise.}
CoreDbInitLegaSetupFn* = proc() {.noRaise.}
CoreDbMiscFns* = object
@ -172,7 +172,7 @@ type
new*: CoreDbConstructorFns
methods*: CoreDbMiscFns
CoreDbError* = object of RootObj
CoreDbErrorRef* = ref object of RootRef
## Generic error object
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
proc validateMethodsDesc(eph: EphemMethodsDesc) =
@ -111,7 +112,7 @@ proc validateMethodsDesc(db: CoreDbRef) =
# Public debugging helpers
# ------------------------------------------------------------------------------
proc validate*(desc: MethodsDesc | EphemMethodsDesc | CoreDbError) =
proc validate*(desc: MethodsDesc | EphemMethodsDesc | CoreDbErrorRef) =
desc.validateMethodsDesc
proc validate*(db: CoreDbRef) =

View File

@ -42,7 +42,7 @@ type
LegacyCoreDbMptBE = ref object of CoreDbMptBackendRef
mpt: HexaryTrie
LegacyCoreDbError = object of CoreDbError
LegacyCoreDbError = ref object of CoreDbErrorRef
ctx: string ## Context where the exception or error occured
name: string ## name of exception
msg: string ## Exception info
@ -124,11 +124,13 @@ proc miscMethods(db: LegacyDbRef): CoreDbMiscFns =
backendFn: proc(): CoreDbBackendRef =
db.bless(LegacyCoreDbBE(base: db)),
errorPrintFn: proc(e: CoreDbError): string =
errorPrintFn: proc(e: CoreDbErrorRef): string =
if not e.isNil:
let e = e.LegacyCoreDbError
result &= "ctx=\"" & $e.ctx & "\"" & " , "
result &= "name=\"" & $e.name & "\"" & ", "
result &= "msg=\"" & $e.msg & "\"",
result &= "msg=\"" & $e.msg & "\""
discard,
legacySetupFn: proc() =
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
CoreDbCaptFlags,
CoreDbError,
CoreDbErrorRef,
CoreDbCaptRef,
CoreDbKvtRef,
CoreDbMptRef,