Fix REST generic error parsing. (#4189)

* Fix REST generic error parser.
* Unescape test vectors.
* Fix RestGenericError writer and tests, to encode `code` as `Number`.
This commit is contained in:
Eugene Kabanov 2022-09-28 21:47:15 +03:00 committed by GitHub
parent c11b30f8e1
commit 8778e1cf8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 327 additions and 118 deletions

View File

@ -328,13 +328,11 @@ proc jsonMsgResponse*(t: typedesc[RestApiResponse],
block:
var default: seq[byte]
try:
var defstrings: seq[string]
var stream = memoryOutput()
var writer = JsonWriter[RestJson].init(stream)
writer.beginRecord()
writer.writeField("code", "200")
writer.writeField("code", 200)
writer.writeField("message", msg)
writer.writeField("stacktraces", defstrings)
writer.endRecord()
stream.getOutput(seq[byte])
except SerializationError:
@ -349,13 +347,11 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
block:
var default: string
try:
var defstrings: seq[string]
var stream = memoryOutput()
var writer = JsonWriter[RestJson].init(stream)
writer.beginRecord()
writer.writeField("code", Base10.toString(uint64(status.toInt())))
writer.writeField("code", int(status.toInt()))
writer.writeField("message", msg)
writer.writeField("stacktraces", defstrings)
writer.endRecord()
stream.getOutput(string)
except SerializationError:
@ -370,16 +366,13 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
block:
var default: string
try:
var defstrings: seq[string]
var stream = memoryOutput()
var writer = JsonWriter[RestJson].init(stream)
writer.beginRecord()
writer.writeField("code", Base10.toString(uint64(status.toInt())))
writer.writeField("code", int(status.toInt()))
writer.writeField("message", msg)
if len(stacktrace) > 0:
writer.writeField("stacktraces", [stacktrace])
else:
writer.writeField("stacktraces", defstrings)
writer.endRecord()
stream.getOutput(string)
except SerializationError:
@ -398,7 +391,7 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
var stream = memoryOutput()
var writer = JsonWriter[RestJson].init(stream)
writer.beginRecord()
writer.writeField("code", Base10.toString(uint64(status.toInt())))
writer.writeField("code", int(status.toInt()))
writer.writeField("message", msg)
writer.writeField("stacktraces", stacktraces)
writer.endRecord()
@ -419,7 +412,7 @@ proc jsonErrorList*(t: typedesc[RestApiResponse],
var stream = memoryOutput()
var writer = JsonWriter[RestJson].init(stream)
writer.beginRecord()
writer.writeField("code", Base10.toString(uint64(status.toInt())))
writer.writeField("code", int(status.toInt()))
writer.writeField("message", msg)
writer.writeField("failures", failures)
writer.endRecord()
@ -2227,6 +2220,59 @@ proc writeValue*(writer: var JsonWriter[RestJson],
writer.writeField("is_optimistic", value.is_optimistic.get())
writer.endRecord()
## RestGenericError
proc readValue*(reader: var JsonReader[RestJson],
value: var RestGenericError) {.
raises: [SerializationError, IOError, Defect].} =
var
code: Opt[uint64]
message: Opt[string]
stacktraces: Option[seq[string]]
for fieldName in readObjectFields(reader):
case fieldName
of "code":
if code.isSome():
reader.raiseUnexpectedField("Multiple `code` fields found",
"RestGenericError")
let ires =
try:
let res = reader.readValue(int)
if res < 0:
reader.raiseUnexpectedValue("Invalid `code` field value")
Opt.some(uint64(res))
except SerializationError:
Opt.none(uint64)
if ires.isNone():
let sres = Base10.decode(uint64, reader.readValue(string)).valueOr:
reader.raiseUnexpectedValue("Invalid `code` field format")
code = Opt.some(sres)
else:
code = ires
of "message":
if message.isSome():
reader.raiseUnexpectedField("Multiple `message` fields found",
"RestGenericError")
message = Opt.some(reader.readValue(string))
of "stacktraces":
if stacktraces.isSome():
reader.raiseUnexpectedField("Multiple `stacktraces` fields found",
"RestGenericError")
stacktraces = some(reader.readValue(seq[string]))
else:
# We ignore all additional fields.
discard reader.readValue(JsonString)
if code.isNone():
reader.raiseUnexpectedValue("Missing or invalid `code` value")
if message.isNone():
reader.raiseUnexpectedValue("Missing or invalid `message` value")
value = RestGenericError(
code: code.get(), message: message.get(),
stacktraces: stacktraces
)
proc parseRoot(value: string): Result[Eth2Digest, cstring] =
try:
ok(Eth2Digest(data: hexToByteArray[32](value)))

View File

@ -101,7 +101,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -150,7 +150,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -163,7 +163,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -265,7 +265,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -308,7 +308,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -321,7 +321,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -414,7 +414,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -427,7 +427,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -470,7 +470,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -483,7 +483,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -596,7 +596,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -609,7 +609,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -622,7 +622,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -635,7 +635,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -648,7 +648,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -661,7 +661,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -674,7 +674,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -687,7 +687,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -700,7 +700,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -947,7 +947,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -960,7 +960,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -973,7 +973,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -986,7 +986,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -999,7 +999,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1012,7 +1012,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1050,7 +1050,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -1063,7 +1063,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -1076,7 +1076,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1089,7 +1089,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1102,7 +1102,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1115,7 +1115,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1128,7 +1128,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1196,7 +1196,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1209,7 +1209,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1283,7 +1283,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -1296,7 +1296,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -1309,7 +1309,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1322,7 +1322,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1335,7 +1335,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1348,7 +1348,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1361,7 +1361,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1374,7 +1374,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1387,7 +1387,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1462,7 +1462,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1474,7 +1474,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1498,7 +1498,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1522,7 +1522,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1534,7 +1534,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1546,7 +1546,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1594,7 +1594,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1606,7 +1606,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1618,7 +1618,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1630,7 +1630,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1642,7 +1642,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1654,7 +1654,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -1738,7 +1738,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1750,7 +1750,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1762,7 +1762,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1862,7 +1862,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1874,7 +1874,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -1886,7 +1886,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -1898,7 +1898,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2037,7 +2037,7 @@
{"key": "Content-Type", "value": "application/json", "operator": "equals"},
{"key": "Eth-Consensus-Version", "operator": "notexists"}
],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2052,7 +2052,7 @@
{"key": "Content-Type", "value": "application/json", "operator": "equals"},
{"key": "Eth-Consensus-Version", "operator": "notexists"}
],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2067,7 +2067,7 @@
{"key": "Content-Type", "value": "application/json", "operator": "equals"},
{"key": "Eth-Consensus-Version", "operator": "notexists"}
],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2082,7 +2082,7 @@
{"key": "Content-Type", "value": "application/json", "operator": "equals"},
{"key": "Eth-Consensus-Version", "operator": "notexists"}
],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2206,7 +2206,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2218,7 +2218,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2230,7 +2230,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2242,7 +2242,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2366,7 +2366,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2378,7 +2378,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2390,7 +2390,7 @@
"response": {
"status": {"operator": "equals", "value": "404"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 404, "message": ""}}]
}
},
{
@ -2502,7 +2502,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2514,7 +2514,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2538,7 +2538,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2550,7 +2550,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2562,7 +2562,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2586,7 +2586,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2598,7 +2598,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2610,7 +2610,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2853,7 +2853,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2865,7 +2865,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2964,7 +2964,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2976,7 +2976,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -2988,7 +2988,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3052,7 +3052,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3064,7 +3064,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3076,7 +3076,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3088,7 +3088,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3100,7 +3100,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3112,7 +3112,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3124,7 +3124,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3136,7 +3136,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3148,7 +3148,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3160,7 +3160,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3198,7 +3198,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3212,7 +3212,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3237,7 +3237,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3251,7 +3251,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3265,7 +3265,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3304,7 +3304,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -3318,7 +3318,7 @@
"response": {
"status": {"operator": "equals", "value": "500"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 500, "message": ""}}]
}
},
{
@ -3332,7 +3332,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3346,7 +3346,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
@ -3360,7 +3360,7 @@
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": "", "message": ""}}]
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{

View File

@ -42,6 +42,7 @@ import # Unit test
./test_zero_signature,
./test_key_splitting,
./test_remote_keystore,
./test_serialization,
./fork_choice/tests_fork_choice,
./consensus_spec/all_tests as consensus_all_tests,
./slashing_protection/test_fixtures,

View File

@ -0,0 +1,162 @@
# beacon_chain
# Copyright (c) 2021-2022 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.used.}
import
std/options,
stew/results, presto/client,
testutils/unittests, chronicles,
../beacon_chain/spec/eth2_apis/[eth2_rest_serialization, rest_types],
./testutil
suite "Serialization/deserialization test suite":
test "RestGenericError parser tests":
proc init(t: typedesc[RestGenericError], status: int,
message: string): RestGenericError =
RestGenericError(
code: uint64(status), message: message,
stacktraces: none[seq[string]]()
)
proc init(t: typedesc[RestGenericError], status: int,
message: string,
stacktraces: openArray[string]): RestGenericError =
RestGenericError(
code: uint64(status), message: message,
stacktraces: some(@stacktraces)
)
const GoodTestVectors = [
(
"""{"code": 500, "message": "block not found"}""",
RestGenericError.init(500, "block not found")
),
(
"""{"code": "600", "message": "block not found"}""",
RestGenericError.init(600, "block not found")
),
(
"""{"code": "700", "message": "block not found",
"data": "data", "custom": "field"}""",
RestGenericError.init(700, "block not found")
),
(
"""{"code":"701", "message": "block not found",
"data": "data", "custom": 300}""",
RestGenericError.init(701, "block not found")
),
(
"""{"code": "702", "message": "block not found",
"data": "data", "custom": {"field1": "value1"}}""",
RestGenericError.init(702, "block not found")
),
(
"""{"code": 800, "message": "block not found",
"custom": "data", "stacktraces": []}""",
RestGenericError.init(800, "block not found", [])
),
(
"""{"code": 801, "message": "block not found",
"custom": 100, "stacktraces": []}""",
RestGenericError.init(801, "block not found", [])
),
(
"""{"code": 802, "message": "block not found",
"custom": {"field1": "value1"}, "stacktraces": []}""",
RestGenericError.init(802, "block not found", [])
),
(
"""{"code": "900", "message": "block not found",
"stacktraces": ["line1", "line2", "line3"], "custom": "data"}""",
RestGenericError.init(900, "block not found",
["line1", "line2", "line3"])
),
(
"""{"code": "901", "message": "block not found",
"stacktraces": ["line1", "line2", "line3"], "custom": 2000}""",
RestGenericError.init(901, "block not found",
["line1", "line2", "line3"])
),
(
"""{"code": "902", "message": "block not found",
"stacktraces": ["line1", "line2", "line3"],
"custom": {"field1": "value1"}}""",
RestGenericError.init(902, "block not found",
["line1", "line2", "line3"])
)
]
const FailureTestVectors = [
# `code` has negative value.
"""{"code":-1, "message": "block not found"}""",
# `code` has negative value encoded as string.
"""{"code": "-1", "message": "block not found"}""",
# `code` field as an object.
"""{"code":{"object": "value"}, "message": "block not found"}""",
# `message` field as number.
"""{"code": "400", "message": 100}""",
# `message` field as an object.
"""{"code": "400", "message": {"object": "value"}}""",
# `stacktraces` field as an object.
"""{"code": "400", "message": "block not found",
"stacktraces":{"object": "value"}}""",
# Field `stacktraces` mixed array values.
"""{"code": "400", "message": "block not found",
"stacktraces":["object", 1]""",
# missing required field `code` and `message`.
"",
# missing required field `message`.
"""{"code":"400"}""",
# missing required field `code`.
"""{"message": "block not found"}"""
]
let contentType = getContentType("application/json").get()
for test in GoodTestVectors:
let res = decodeBytes(
RestGenericError, test[0].toOpenArrayByte(0, len(test[0]) - 1),
Opt.some(contentType))
check res.isOk()
let response = res.get()
check:
response.code == test[1].code
response.message == test[1].message
if response.stacktraces.isNone():
check test[1].stacktraces.isNone()
else:
check:
test[1].stacktraces.isSome()
test[1].stacktraces.get() == response.stacktraces.get()
for test in FailureTestVectors:
let res = decodeBytes(
RestGenericError, test.toOpenArrayByte(0, len(test) - 1),
Opt.some(contentType))
check res.isErr()
test "RestGenericError writer tests":
proc `==`(a: RestApiResponse, b: string): bool =
case a.kind
of RestApiResponseKind.Content:
a.content.data.bytesToString() == b
of RestApiResponseKind.Error:
a.errobj.message == b
else:
raiseAssert "Unsupported RestApiResponse kind"
check:
jsonMsgResponse(RestApiResponse, "data") ==
"""{"code":200,"message":"data"}"""
jsonError(RestApiResponse, Http202, "data") ==
"""{"code":202,"message":"data"}"""
jsonError(RestApiResponse, Http400, "data", "") ==
"""{"code":400,"message":"data"}"""
jsonError(RestApiResponse, Http404, "data", "stacktrace") ==
"""{"code":404,"message":"data","stacktraces":["stacktrace"]}"""
jsonError(RestApiResponse, Http500, "data", ["s1", "s2"]) ==
"""{"code":500,"message":"data","stacktraces":["s1","s2"]}"""
jsonErrorList(RestApiResponse, Http408, "data", ["s1", "s2"]) ==
"""{"code":408,"message":"data","failures":["s1","s2"]}"""