Skip to content
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

Replace Skip link errors with ElementError #4219

Merged
merged 4 commits into from
Sep 19, 2023

Conversation

colinrotherham
Copy link
Contributor

This PR replaces Skip link $module and $module.hash errors with ElementError to close:

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4219 September 18, 2023 17:22 Inactive
@github-actions
Copy link

github-actions bot commented Sep 18, 2023

📋 Stats

File sizes

File Size
dist/govuk-frontend-4.6.0.min.css 118.07 KiB
dist/govuk-frontend-4.6.0.min.js 47.1 KiB
dist/govuk-frontend-ie8-4.6.0.min.css 79.27 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 75.84 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 71.24 KiB
packages/govuk-frontend/dist/govuk/all.mjs 3.84 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 359 B
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 36.79 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.83 KiB

Modules

File Size
all.mjs 67.34 KiB
components/accordion/accordion.mjs 21.9 KiB
components/button/button.mjs 4.71 KiB
components/character-count/character-count.mjs 21.64 KiB
components/checkboxes/checkboxes.mjs 5.41 KiB
components/error-summary/error-summary.mjs 6.01 KiB
components/exit-this-page/exit-this-page.mjs 16.6 KiB
components/header/header.mjs 3.34 KiB
components/notification-banner/notification-banner.mjs 4.54 KiB
components/radios/radios.mjs 4.41 KiB
components/skip-link/skip-link.mjs 3.73 KiB
components/tabs/tabs.mjs 9.07 KiB

View stats and visualisations on the review app


Action run for 70265f1

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4219 September 18, 2023 17:27 Inactive
Comment on lines -24 to +23
* @throws {MissingElementError} If the element with the specified ID is not found
* @throws {ElementError} when $module is not set or the wrong type
* @throws {ElementError} when $module.hash does not contain a hash
* @throws {ElementError} when the linked element is missing or the wrong type
Copy link
Contributor Author

@colinrotherham colinrotherham Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romaricpascal Just flagging that @domoscargin has documented @throw for JSDoc output

We only need them on public methods but I've updated them based on the test names

Presume this is something we'd want to roll out elsewhere after #4072

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we'll want all our errors documented with @throws on the constructors once we've got the full extent of what's being thrown and the wordings.

Comment on lines -40 to -43
try {
const $linkedElement = this.getLinkedElement()
this.$linkedElement = $linkedElement
} catch (error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@domoscargin It got a bit tricky to handle all the error variations in a try/catch

Now that we have ElementError we can throw the same error type

Copy link
Contributor

@domoscargin domoscargin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I have a query, but it's non-blocking.

packages/govuk-frontend/src/govuk/errors/index.mjs Outdated Show resolved Hide resolved
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4219 September 19, 2023 08:24 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers for me, though we may revisit messages once we have a final wording in #4072.

Comment on lines -24 to +23
* @throws {MissingElementError} If the element with the specified ID is not found
* @throws {ElementError} when $module is not set or the wrong type
* @throws {ElementError} when $module.hash does not contain a hash
* @throws {ElementError} when the linked element is missing or the wrong type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we'll want all our errors documented with @throws on the constructors once we've got the full extent of what's being thrown and the wordings.

Comment on lines +53 to +57
throw new ElementError(this.$module, {
componentName: 'Skip link',
identifier: '$module.hash',
expectedType: 'string'
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remark Clever way to sort an "is not of the expected type" message. I think that's fine for this PR, but we may have to revisit once we settle on wording for the error messages depending on how things end up worded. Depending on our public API for errors, that might even be able to wait post v5, and be all internal anyways.

Comment on lines +121 to +126
getFragmentFromUrl(url) {
if (url.indexOf('#') === -1) {
return undefined
}

return this.$module.hash.split('#').pop()
return url.split('#').pop()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout! 🙌🏻 Looks like a little function ripe for no longer living in these classes now they're both aligned (especially as it does not reference this).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😊

name: 'MissingElementError',
message: 'Skip link: $module "href" attribute does not contain a hash'
name: 'ElementError',
message: 'Skip link: $module.hash is not of type "string"'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging for #4072 that we could harmonise the message for when an element is missing to " is missing" and use this here instead of discussing the type. Up for discussion when tackling that later issue 😊

@colinrotherham colinrotherham merged commit 261987d into main Sep 19, 2023
44 checks passed
@colinrotherham colinrotherham deleted the missing-to-element-error branch September 19, 2023 13:17
colinrotherham added a commit that referenced this pull request Sep 27, 2023
Replace Skip link errors with `ElementError`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants