-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: wrap Intl.<>() calls in try/catch #297
Conversation
src/relative-time-element.ts
Outdated
let dateTimeFormat | ||
try { | ||
dateTimeFormat = new Intl.DateTimeFormat(this.#lang, { | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
hour: 'numeric', | ||
minute: '2-digit', | ||
timeZoneName: 'short', | ||
}) | ||
} catch (_e) { | ||
dateTimeFormat = new Intl.DateTimeFormat('default', { | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
hour: 'numeric', | ||
minute: '2-digit', | ||
timeZoneName: 'short', | ||
}) | ||
} | ||
return dateTimeFormat.format(date) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keithamus I did some research on using an array in the constructor instead but it'd fail anyways if one of the locales was invalid, so I settled on this.
Also I'm not quite sure how to test this but happy to update on Primer and test once this is in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of try/catching each of the Intl
calls we could add a try/catch into #lang
:
get #lang() {
const lang = this.closest('[lang]')?.getAttribute('lang') ||
this.ownerDocument.documentElement.getAttribute('lang')
try {
return new Intl.Locale(lang).toString()
} catch {
return 'default'
}
}
This way #lang
would always return a reliable language code and we minimise the surface area of changes.
@keithamus done in 0a42e68, much better now, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, some tests might be interesting. Also you’ll need someone from Primer to approve as I am no longer in the reviewers team 🥹
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, one nit:
Co-authored-by: Keith Cirkel <[email protected]>
…#6559) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@github/relative-time-element](https://github.com/github/relative-time-element) | dependencies | patch | [`4.4.4` -> `4.4.5`](https://renovatebot.com/diffs/npm/@github%2frelative-time-element/4.4.4/4.4.5) | --- ### Release Notes <details> <summary>github/relative-time-element (@​github/relative-time-element)</summary> ### [`v4.4.5`](https://github.com/github/relative-time-element/releases/tag/v4.4.5) [Compare Source](github/relative-time-element@v4.4.4...v4.4.5) #### What's Changed - fix: wrap Intl.<>() calls in try/catch by [@​francinelucca](https://github.com/francinelucca) in github/relative-time-element#297 - get main branch green by [@​keithamus](https://github.com/keithamus) in github/relative-time-element#302 - Make `applyDuration` reversible by [@​leduyquang753](https://github.com/leduyquang753) in github/relative-time-element#298 - Use node v22 by [@​camertron](https://github.com/camertron) in github/relative-time-element#303 #### New Contributors - [@​francinelucca](https://github.com/francinelucca) made their first contribution in github/relative-time-element#297 - [@​leduyquang753](https://github.com/leduyquang753) made their first contribution in github/relative-time-element#298 - [@​camertron](https://github.com/camertron) made their first contribution in github/relative-time-element#303 **Full Changelog**: github/relative-time-element@v4.4.4...v4.4.5 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "* 0-3 * * *" (UTC), Automerge - "* 0-3 * * *" (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xMDYuMCIsInVwZGF0ZWRJblZlciI6IjM5LjEwNi4wIiwidGFyZ2V0QnJhbmNoIjoiZm9yZ2VqbyIsImxhYmVscyI6WyJkZXBlbmRlbmN5LXVwZ3JhZGUiLCJ0ZXN0L25vdC1uZWVkZWQiXX0=--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6559 Reviewed-by: Otto <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
Closes https://github.com/github/primer/issues/4409
Fixes error thrown when an invalid locale is passed to the
Intl
functions by wrapping it in Try/Catch. When the call fails (locale is invalid), the default locale will be used