From a3c7851d920298db47e76199fc8339601134ab96 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Mon, 11 Oct 2021 21:23:30 +0200 Subject: [PATCH] Changed processDate function to throw if it cannot complete conversion --- src/PKPass.ts | 16 ++++++---------- src/utils.ts | 7 +++---- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index abfec4e..181c2f4 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -698,15 +698,13 @@ export default class PKPass extends Bundle { return; } - const parsedDate = processDate("expirationDate", date); - - if (!parsedDate) { + try { + this[propsSymbol]["expirationDate"] = processDate(date); + } catch (err) { throw new TypeError( `Cannot set expirationDate. Invalid date ${date}`, ); } - - this[propsSymbol]["expirationDate"] = parsedDate; } /** @@ -789,15 +787,13 @@ export default class PKPass extends Bundle { return; } - const parsedDate = processDate("relevantDate", date); - - if (!parsedDate) { + try { + this[propsSymbol]["relevantDate"] = processDate(date); + } catch (err) { throw new TypeError( `Cannot set relevantDate. Invalid date ${date}`, ); } - - this[propsSymbol]["relevantDate"] = parsedDate; } /** diff --git a/src/utils.ts b/src/utils.ts index 38406eb..d612034 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,16 +26,15 @@ export function isValidRGB(value?: string): boolean { return rgb.slice(1, 4).every((v) => Math.abs(Number(v)) <= 255); } -export function processDate(key: string, date: Date): string | null { +export function processDate(date: Date): string | null { if (!(date instanceof Date)) { - return null; + throw "Invalid date"; } const dateParse = dateToW3CString(date); if (!dateParse) { - console.warn(formatMessage(DEBUG.DATE_FORMAT_UNMATCH, key)); - return null; + throw "Invalid date"; } return dateParse;