From b6bc71741620b789296a59cec17f2a57ee2dd857 Mon Sep 17 00:00:00 2001 From: Natty Date: Tue, 24 Dec 2024 02:49:39 +0100 Subject: [PATCH] Implemented handling for RPC error returns --- packages/backend/src/mag/rpc-client.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/mag/rpc-client.ts b/packages/backend/src/mag/rpc-client.ts index 4e772fc01e..7b7f0dd452 100644 --- a/packages/backend/src/mag/rpc-client.ts +++ b/packages/backend/src/mag/rpc-client.ts @@ -64,21 +64,26 @@ async function getRpcClient(): Promise { return; } - const header = buf.readUInt8(); - if (header != 77) { + const header = String.fromCharCode(buf.readUInt8()); + if (!["M", "F"].includes(header)) { logger.error(`Invalid header: ${header}`); return; } const serial = buf.readBigUInt64BE(); - const dataLen = buf.readUInt32BE(); - if (buf.remaining() < dataLen) { - buf.readOffset = 0; - return; + let dataDecoded; + if (header === "M") { + const dataLen = buf.readUInt32BE(); + if (buf.remaining() < dataLen) { + buf.readOffset = 0; + return; + } + const data = buf.readBuffer(dataLen); + dataDecoded = decode(data); + } else { + dataDecoded = { success: false, data: "RPC returned failure" }; } - const data = buf.readBuffer(dataLen); - const dataDecoded: any = decode(data); // Move the rest of the data to the beginning of the buffer const rest = buf.readBuffer();