Allow custom serializers to report unexpected values in the Json file

This commit is contained in:
Zahary Karadjov 2020-05-30 22:40:17 +03:00
parent aa51f6fed1
commit 8d4c31f7c2
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 16 additions and 4 deletions

View File

@ -35,10 +35,12 @@ type
deserializedField*: string
innerException*: ref CatchableError
UnexpectedToken* = object of JsonReaderError
UnexpectedTokenError* = object of JsonReaderError
encountedToken*: TokKind
expectedToken*: ExpectedTokenCategory
UnexpectedValueError* = object of JsonReaderError
IntOverflowError* = object of JsonReaderError
isNegative: bool
absIntVal: uint64
@ -54,7 +56,7 @@ method formatMsg*(err: ref JsonReaderError, filename: string): string =
method formatMsg*(err: ref UnexpectedField, filename: string): string =
fmt"{filename}({err.line}, {err.col}) Unexpected field '{err.encounteredField}' while deserializing {err.deserializedType}"
method formatMsg*(err: ref UnexpectedToken, filename: string): string =
method formatMsg*(err: ref UnexpectedTokenError, filename: string): string =
fmt"{filename}({err.line}, {err.col}) Unexpected token '{err.encountedToken}' in place of '{err.expectedToken}'"
method formatMsg*(err: ref GenericJsonReaderError, filename: string): string =
@ -63,17 +65,27 @@ method formatMsg*(err: ref GenericJsonReaderError, filename: string): string =
method formatMsg*(err: ref IntOverflowError, filename: string): string =
fmt"{filename}({err.line}, {err.col}) The value '{err.valueStr}' is outside of the allowed range"
method formatMsg*(err: ref UnexpectedValueError, filename: string): string =
fmt"{filename}({err.line}, {err.col}) {err.msg}"
proc assignLineNumber*(ex: ref JsonReaderError, r: JsonReader) =
ex.line = r.lexer.line
ex.col = r.lexer.tokenStartCol
proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory) {.noreturn.} =
var ex = new UnexpectedToken
proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory)
{.noreturn.} =
var ex = new UnexpectedTokenError
ex.assignLineNumber(r)
ex.encountedToken = r.lexer.tok
ex.expectedToken = expected
raise ex
proc raiseUnexpectedValue*(r: JsonReader, msg: string) {.noreturn.} =
var ex = new UnexpectedValueError
ex.assignLineNumber(r)
ex.msg = msg
raise ex
proc raiseIntOverflow*(r: JsonReader, absIntVal: uint64, isNegative: bool) {.noreturn.} =
var ex = new IntOverflowError
ex.assignLineNumber(r)