From 1115b72ca649b6b39c5402cf6bb24ffd448dbbae Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sun, 7 Apr 2024 02:30:45 -1000 Subject: [PATCH] Fix JSON parse error with Megalodon --- packages/log-manager/index.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/log-manager/index.ts b/packages/log-manager/index.ts index 5db5f37e..94989e6c 100644 --- a/packages/log-manager/index.ts +++ b/packages/log-manager/index.ts @@ -95,13 +95,17 @@ export class LogManager { const content_type = req.headers.get("Content-Type"); if (content_type?.includes("application/json")) { - const json = await req.json(); - const stringified = JSON.stringify(json, null, 4) - .split("\n") - .map((line) => ` ${line}`) - .join("\n"); + try { + const json = await req.json(); + const stringified = JSON.stringify(json, null, 4) + .split("\n") + .map((line) => ` ${line}`) + .join("\n"); - string += `${stringified}\n`; + string += `${stringified}\n`; + } catch { + string += " [Invalid JSON]\n"; + } } else if ( content_type && (content_type.includes("application/x-www-form-urlencoded") ||