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 <abias1122@gmail.com>

---------

Co-authored-by: Beehaw Dev <dev@beehaw.dev>
Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
This commit is contained in:
Security-Chief-Odo 2023-08-26 09:35:10 -05:00 committed by GitHub
parent 7f993c6ce7
commit 02bb1b84e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,8 +2,23 @@ import formatDistanceStrict from "date-fns/formatDistanceStrict";
import parseISO from "date-fns/parseISO"; import parseISO from "date-fns/parseISO";
export default function (dateString?: string) { export default function (dateString?: string) {
const parsed = parseISO((dateString ?? Date.now().toString()) + "Z"); if (!dateString) {
console.error(err);
return "DATE ERROR";
}
try {
const parsed = parseISO(Date.now().toString() + "Z");
return formatDistanceStrict(parsed, new Date(), { return formatDistanceStrict(parsed, new Date(), {
addSuffix: true, 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";
}
}
} }