Allow custom serializers to report unexpected values in the Json file
This commit is contained in:
parent
aa51f6fed1
commit
8d4c31f7c2
|
@ -35,10 +35,12 @@ type
|
||||||
deserializedField*: string
|
deserializedField*: string
|
||||||
innerException*: ref CatchableError
|
innerException*: ref CatchableError
|
||||||
|
|
||||||
UnexpectedToken* = object of JsonReaderError
|
UnexpectedTokenError* = object of JsonReaderError
|
||||||
encountedToken*: TokKind
|
encountedToken*: TokKind
|
||||||
expectedToken*: ExpectedTokenCategory
|
expectedToken*: ExpectedTokenCategory
|
||||||
|
|
||||||
|
UnexpectedValueError* = object of JsonReaderError
|
||||||
|
|
||||||
IntOverflowError* = object of JsonReaderError
|
IntOverflowError* = object of JsonReaderError
|
||||||
isNegative: bool
|
isNegative: bool
|
||||||
absIntVal: uint64
|
absIntVal: uint64
|
||||||
|
@ -54,7 +56,7 @@ method formatMsg*(err: ref JsonReaderError, filename: string): string =
|
||||||
method formatMsg*(err: ref UnexpectedField, 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}"
|
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}'"
|
fmt"{filename}({err.line}, {err.col}) Unexpected token '{err.encountedToken}' in place of '{err.expectedToken}'"
|
||||||
|
|
||||||
method formatMsg*(err: ref GenericJsonReaderError, filename: string): string =
|
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 =
|
method formatMsg*(err: ref IntOverflowError, filename: string): string =
|
||||||
fmt"{filename}({err.line}, {err.col}) The value '{err.valueStr}' is outside of the allowed range"
|
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) =
|
proc assignLineNumber*(ex: ref JsonReaderError, r: JsonReader) =
|
||||||
ex.line = r.lexer.line
|
ex.line = r.lexer.line
|
||||||
ex.col = r.lexer.tokenStartCol
|
ex.col = r.lexer.tokenStartCol
|
||||||
|
|
||||||
proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory) {.noreturn.} =
|
proc raiseUnexpectedToken*(r: JsonReader, expected: ExpectedTokenCategory)
|
||||||
var ex = new UnexpectedToken
|
{.noreturn.} =
|
||||||
|
var ex = new UnexpectedTokenError
|
||||||
ex.assignLineNumber(r)
|
ex.assignLineNumber(r)
|
||||||
ex.encountedToken = r.lexer.tok
|
ex.encountedToken = r.lexer.tok
|
||||||
ex.expectedToken = expected
|
ex.expectedToken = expected
|
||||||
raise ex
|
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.} =
|
proc raiseIntOverflow*(r: JsonReader, absIntVal: uint64, isNegative: bool) {.noreturn.} =
|
||||||
var ex = new IntOverflowError
|
var ex = new IntOverflowError
|
||||||
ex.assignLineNumber(r)
|
ex.assignLineNumber(r)
|
||||||
|
|
Loading…
Reference in New Issue