Skip to content

Commit

Permalink
fix: more readable error message when no valid fields specified (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Mar 1, 2024
1 parent fb59cdb commit 51f2c40
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 27 additions & 0 deletions packages/temporal-polyfill/src/impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/bagRefine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/temporal-polyfill/src/internal/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/isoParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 51f2c40

Please sign in to comment.