From 02bb1b84e1220cbf25b25eaabf1017083906b277 Mon Sep 17 00:00:00 2001 From: Security-Chief-Odo <87156706+Security-Chief-Odo@users.noreply.github.com> Date: Sat, 26 Aug 2023 09:35:10 -0500 Subject: [PATCH] Added error handling to format-past-date to prevent timeouts on modlog (#2083) * Added error handling to format-past-date to prevent timeouts on modlog * Apply suggestions from code review Co-authored-by: SleeplessOne1917 --------- Co-authored-by: Beehaw Dev Co-authored-by: SleeplessOne1917 --- src/shared/utils/helpers/format-past-date.ts | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/shared/utils/helpers/format-past-date.ts b/src/shared/utils/helpers/format-past-date.ts index 5bef4e83..1e0ffda8 100644 --- a/src/shared/utils/helpers/format-past-date.ts +++ b/src/shared/utils/helpers/format-past-date.ts @@ -2,8 +2,23 @@ import formatDistanceStrict from "date-fns/formatDistanceStrict"; import parseISO from "date-fns/parseISO"; export default function (dateString?: string) { - const parsed = parseISO((dateString ?? Date.now().toString()) + "Z"); - return formatDistanceStrict(parsed, new Date(), { - addSuffix: true, - }); + if (!dateString) { + console.error(err); + return "DATE ERROR"; + } + + try { + const parsed = parseISO(Date.now().toString() + "Z"); + return formatDistanceStrict(parsed, new Date(), { + addSuffix: true, + }); + } catch (err) { + console.error(err); + if (err instanceof RangeError) { + console.error( + `Got the invalid value of ${dateString} when attempting to parse to ISO date`, + ); + return "DATE ERROR"; + } + } }