From 51f2c40be9898f75b57f73b743a1ce25dd1241b3 Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Thu, 29 Feb 2024 19:59:22 -0500 Subject: [PATCH] fix: more readable error message when no valid fields specified (#30) --- CHANGELOG.md | 1 + packages/temporal-polyfill/src/impl.test.ts | 27 +++++++++++++++++++ .../src/internal/bagRefine.ts | 2 +- .../src/internal/errorMessages.ts | 3 ++- .../src/internal/isoParse.ts | 2 +- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fdd9ff9..6b468378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ v0.2.3 (2024-02-29) ------------------- +- fix: more readable error message when no valid fields specified (#30) - fix: non-iso/gregory calendars dayOfYear/weekOfYear off-by-one - conformance to latest spec - yearOfWeek/weekOfYear should return undefined for non-iso/gregory calendars diff --git a/packages/temporal-polyfill/src/impl.test.ts b/packages/temporal-polyfill/src/impl.test.ts index 0a72f4b3..1cd30a86 100644 --- a/packages/temporal-polyfill/src/impl.test.ts +++ b/packages/temporal-polyfill/src/impl.test.ts @@ -7,6 +7,33 @@ describe('Temporal.Duration', () => { const s = d.toLocaleString(d) expect(s).toBeTruthy() }) + + it('gives readable error message when no valid field', () => { + let error: TypeError | undefined + + try { + const d = Temporal.Duration.from({ day: 5 }) + expect(d).toBeTruthy() // won't reach + } catch (e: any) { + error = e + } + + expect(error).toBeInstanceOf(TypeError) + expect(error!.toString()).toMatch( + [ + 'days', + 'hours', + 'microseconds', + 'milliseconds', + 'minutes', + 'months', + 'nanoseconds', + 'seconds', + 'weeks', + 'years', + ].join(','), + ) + }) }) describe('Intl.DateTimeFormat', () => { diff --git a/packages/temporal-polyfill/src/internal/bagRefine.ts b/packages/temporal-polyfill/src/internal/bagRefine.ts index c324aa7a..d2fdd124 100644 --- a/packages/temporal-polyfill/src/internal/bagRefine.ts +++ b/packages/temporal-polyfill/src/internal/bagRefine.ts @@ -421,7 +421,7 @@ function refineFields( // only check zero fields during .with() calls // for .from() calls, empty-bag-checking will happen within the CalendarImpl if (disallowEmpty && !anyMatching) { - throw new TypeError(errorMessages.noValidFields) + throw new TypeError(errorMessages.noValidFields(validFieldNames)) } return res diff --git a/packages/temporal-polyfill/src/internal/errorMessages.ts b/packages/temporal-polyfill/src/internal/errorMessages.ts index a94b8926..a8112b23 100644 --- a/packages/temporal-polyfill/src/internal/errorMessages.ts +++ b/packages/temporal-polyfill/src/internal/errorMessages.ts @@ -26,7 +26,8 @@ export const forbiddenField = (fieldName: string) => `Invalid field ${fieldName}` export const duplicateFields = (fieldName: string) => `Duplicate field ${fieldName}` -export const noValidFields = 'No valid fields' +export const noValidFields = (validFields: string[]) => + 'No valid fields: ' + validFields.join() export const invalidBag = 'Invalid bag' // Class-related diff --git a/packages/temporal-polyfill/src/internal/isoParse.ts b/packages/temporal-polyfill/src/internal/isoParse.ts index f2eaab2b..da0ea87c 100644 --- a/packages/temporal-polyfill/src/internal/isoParse.ts +++ b/packages/temporal-polyfill/src/internal/isoParse.ts @@ -595,7 +595,7 @@ function organizeDurationParts(parts: string[]): DurationFields { } as DurationFields if (!hasAny) { - throw new RangeError(errorMessages.noValidFields) + throw new RangeError(errorMessages.noValidFields(durationFieldNamesAsc)) } if (parseSign(parts[1]) < 0) {