format ErrorMsg messages reasonably (#1109)

This commit is contained in:
Jacek Sieka 2020-06-04 08:19:25 +02:00 committed by GitHub
parent 22bd8951fd
commit bcbfa736c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -357,12 +357,22 @@ template errorMsgLit(x: static string): ErrorMsg =
const val = ErrorMsg toBytes(x)
val
proc formatErrorMsg(msg: ErrorMSg): string =
let candidate = string.fromBytes(asSeq(msg))
for c in candidate:
# TODO UTF-8 - but let's start with ASCII
if ord(c) < 32 or ord(c) > 127:
return byteutils.toHex(asSeq(msg))
return candidate
proc sendErrorResponse(peer: Peer,
conn: Connection,
noSnappy: bool,
responseCode: ResponseCode,
errMsg: ErrorMsg) {.async.} =
debug "Error processing request", peer, responseCode, errMsg
debug "Error processing request",
peer, responseCode, errMsg = formatErrorMsg(errMsg)
await conn.writeChunk(some responseCode, SSZ.encode(errMsg), noSnappy)
proc sendNotificationMsg(peer: Peer, protocolId: string, requestBytes: Bytes) {.async} =