From ddfdcdccd365a7219639ef9e5f405289ab153368 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:38:53 +0300 Subject: [PATCH] Don't crash on ZodError from validateAndParseMessageContent() --- backend/src/utils.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/src/utils.ts b/backend/src/utils.ts index 0c243941..8a7f7c55 100644 --- a/backend/src/utils.ts +++ b/backend/src/utils.ts @@ -44,7 +44,7 @@ import { ChannelTypeStrings } from "./types"; import { sendDM } from "./utils/sendDM"; import { waitForButtonConfirm } from "./utils/waitForInteraction"; import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils"; -import { z } from "zod"; +import { z, ZodError } from "zod"; const fsp = fs.promises; @@ -432,7 +432,16 @@ export function validateAndParseMessageContent(input: unknown): StrictMessageCon dropNullValuesRecursively(input); - return (zStrictMessageContent.parse(input) as unknown) as StrictMessageContent; + try { + return (zStrictMessageContent.parse(input) as unknown) as StrictMessageContent; + } catch (err) { + if (err instanceof ZodError) { + // TODO: Allow error to be thrown and handle at use location + return {}; + } + + throw err; + } } function dropNullValuesRecursively(obj: any) {