2024-02-07 13:41:37 +11:00
|
|
|
import std/sets
|
|
|
|
|
|
2024-02-07 14:39:52 +11:00
|
|
|
import ./stdjson
|
2024-02-07 13:41:37 +11:00
|
|
|
import ./types
|
|
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
|
|
proc mapErrTo*[E1: ref CatchableError, E2: SerdeError](
|
2024-02-08 09:18:55 +11:00
|
|
|
e1: E1, _: type E2, msg: string = e1.msg
|
|
|
|
|
): ref E2 =
|
2024-02-07 13:41:37 +11:00
|
|
|
return newException(E2, msg, e1)
|
|
|
|
|
|
|
|
|
|
proc newSerdeError*(msg: string): ref SerdeError =
|
|
|
|
|
newException(SerdeError, msg)
|
|
|
|
|
|
|
|
|
|
proc newUnexpectedKindError*(
|
2024-02-08 09:18:55 +11:00
|
|
|
expectedType: type, expectedKinds: string, json: JsonNode
|
|
|
|
|
): ref UnexpectedKindError =
|
|
|
|
|
let kind =
|
|
|
|
|
if json.isNil:
|
|
|
|
|
"nil"
|
|
|
|
|
else:
|
|
|
|
|
$json.kind
|
|
|
|
|
newException(
|
|
|
|
|
UnexpectedKindError,
|
|
|
|
|
"deserialization to " & $expectedType & " failed: expected " & expectedKinds &
|
|
|
|
|
" but got " & $kind,
|
|
|
|
|
)
|
2024-02-07 13:41:37 +11:00
|
|
|
|
|
|
|
|
proc newUnexpectedKindError*(
|
2024-02-08 09:18:55 +11:00
|
|
|
expectedType: type, expectedKinds: set[JsonNodeKind], json: JsonNode
|
|
|
|
|
): ref UnexpectedKindError =
|
2024-02-07 13:41:37 +11:00
|
|
|
newUnexpectedKindError(expectedType, $expectedKinds, json)
|
|
|
|
|
|
|
|
|
|
proc newUnexpectedKindError*(
|
2024-02-08 09:18:55 +11:00
|
|
|
expectedType: type, expectedKind: JsonNodeKind, json: JsonNode
|
|
|
|
|
): ref UnexpectedKindError =
|
2024-02-07 13:41:37 +11:00
|
|
|
newUnexpectedKindError(expectedType, {expectedKind}, json)
|