Skip to content

Commit 682264c

Browse files
committed
fix: wrap Intl.<>() calls in try/catch
1 parent bc1b8b7 commit 682264c

File tree

1 file changed

+59
-22
lines changed

1 file changed

+59
-22
lines changed

src/relative-time-element.ts

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,27 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
120120
//
121121
// Returns a formatted time String.
122122
#getFormattedTitle(date: Date): string | undefined {
123-
return new Intl.DateTimeFormat(this.#lang, {
124-
day: 'numeric',
125-
month: 'short',
126-
year: 'numeric',
127-
hour: 'numeric',
128-
minute: '2-digit',
129-
timeZoneName: 'short',
130-
}).format(date)
123+
let dateTimeFormat
124+
try {
125+
dateTimeFormat = new Intl.DateTimeFormat(this.#lang, {
126+
day: 'numeric',
127+
month: 'short',
128+
year: 'numeric',
129+
hour: 'numeric',
130+
minute: '2-digit',
131+
timeZoneName: 'short',
132+
})
133+
} catch (_e) {
134+
dateTimeFormat = new Intl.DateTimeFormat('default', {
135+
day: 'numeric',
136+
month: 'short',
137+
year: 'numeric',
138+
hour: 'numeric',
139+
minute: '2-digit',
140+
timeZoneName: 'short',
141+
})
142+
}
143+
return dateTimeFormat.format(date)
131144
}
132145

133146
#resolveFormat(duration: Duration): ResolvedFormat {
@@ -172,10 +185,20 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
172185
}
173186

174187
#getRelativeFormat(duration: Duration): string {
175-
const relativeFormat = new Intl.RelativeTimeFormat(this.#lang, {
176-
numeric: 'auto',
177-
style: this.formatStyle,
178-
})
188+
let relativeFormat
189+
190+
try {
191+
relativeFormat = new Intl.RelativeTimeFormat(this.#lang, {
192+
numeric: 'auto',
193+
style: this.formatStyle,
194+
})
195+
} catch (_e) {
196+
relativeFormat = new Intl.RelativeTimeFormat('default', {
197+
numeric: 'auto',
198+
style: this.formatStyle,
199+
})
200+
}
201+
179202
const tense = this.tense
180203
if (tense === 'future' && duration.sign !== 1) duration = emptyDuration
181204
if (tense === 'past' && duration.sign !== -1) duration = emptyDuration
@@ -187,16 +210,30 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
187210
}
188211

189212
#getDateTimeFormat(date: Date): string {
190-
const formatter = new Intl.DateTimeFormat(this.#lang, {
191-
second: this.second,
192-
minute: this.minute,
193-
hour: this.hour,
194-
weekday: this.weekday,
195-
day: this.day,
196-
month: this.month,
197-
year: this.year,
198-
timeZoneName: this.timeZoneName,
199-
})
213+
let formatter
214+
try {
215+
formatter = new Intl.DateTimeFormat(this.#lang, {
216+
second: this.second,
217+
minute: this.minute,
218+
hour: this.hour,
219+
weekday: this.weekday,
220+
day: this.day,
221+
month: this.month,
222+
year: this.year,
223+
timeZoneName: this.timeZoneName,
224+
})
225+
} catch (_e) {
226+
formatter = new Intl.DateTimeFormat('default', {
227+
second: this.second,
228+
minute: this.minute,
229+
hour: this.hour,
230+
weekday: this.weekday,
231+
day: this.day,
232+
month: this.month,
233+
year: this.year,
234+
timeZoneName: this.timeZoneName,
235+
})
236+
}
200237
return `${this.prefix} ${formatter.format(date)}`.trim()
201238
}
202239

0 commit comments

Comments
 (0)