From 0657fac98d97d5db020289ba14d86bb4682bea19 Mon Sep 17 00:00:00 2001 From: LJNeon <23249107+LJNeon@users.noreply.github.com> Date: Sun, 2 Oct 2022 18:52:38 -0700 Subject: [PATCH] Better error handling --- src/networking/messageHandler.ts | 35 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/networking/messageHandler.ts b/src/networking/messageHandler.ts index ca94b76..48d122d 100644 --- a/src/networking/messageHandler.ts +++ b/src/networking/messageHandler.ts @@ -7,38 +7,39 @@ import { fileChangeEventToMsg } from "./messageGenerators"; import type { Signal } from "signal-js"; import { Message } from "../interfaces"; -function deserialize(data: RawData): Message | void { - let msg; +function deserialize(data: RawData): Message { + const msg = JSON.parse(data.toString()); - try { - msg = JSON.parse(data.toString()); - } catch (err) { - return console.log(err); + if (typeof msg.jsonrpc !== "string" || msg.jsonrpc !== "2.0" || typeof msg.id !== "number") { + throw Error("Malformed data received."); } - if (typeof msg.jsonrpc !== "string" || msg.jsonrpc !== "2.0" || typeof msg.id !== "number") return; - const id: number = msg.id; const request = messageTracker.get(id); - if (typeof request?.method !== "string") return; - else if (msg.error != null) return { jsonrpc: "2.0", error: msg.error, id }; - else if (msg.result == null) return; + if (typeof request?.method !== "string") { + throw Error("Malformed JSON received."); + } else if (msg.error != null) { + throw Error(msg.error); + } else if (msg.result == null) { + throw Error("Malformed JSON received."); + } return { jsonrpc: "2.0", method: request.method, result: msg.result, id }; } -function isStringArray(s: Array): s is string[] { +export function isStringArray(s: Array): s is string[] { return s.every((s) => typeof s === "string"); } export function messageHandler(signaller: Signal, data: RawData, paths: Map) { - const incoming = deserialize(data); + let incoming; - if (incoming == null) { - return console.log("Malformed data received."); - } else if (incoming.error) { - return console.log(incoming.error); + try { + incoming = deserialize(data); + } catch (err) { + if (err instanceof Error) return console.log(err.message); + else throw err; } switch (incoming.method) {