File tree Expand file tree Collapse file tree
packages/govuk-frontend/src/govuk/errors Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,6 +52,15 @@ describe('errors', () => {
5252 'GOV.UK Frontend initialised without `<script type="module">`'
5353 )
5454 } )
55+
56+ it ( 'allows a custom message to be provided' , ( ) => {
57+ const supportMessage = 'Support for "navigator.clipboard" required'
58+
59+ expect ( new SupportError ( supportMessage ) ) . toHaveProperty (
60+ 'message' ,
61+ supportMessage
62+ )
63+ } )
5564 } )
5665
5766 describe ( 'InitError' , ( ) => {
Original file line number Diff line number Diff line change 1- import { formatErrorMessage , isObject } from '../common/index.mjs'
1+ import { formatErrorMessage , isObject , isScope } from '../common/index.mjs'
22
33/**
44 * GOV.UK Frontend error
@@ -33,19 +33,20 @@ export class SupportError extends GOVUKFrontendError {
3333 /**
3434 * Checks if GOV.UK Frontend is supported on this page
3535 *
36- * @param {HTMLElement | null } [$scope ] - HTML element `<body>` checked for browser support
36+ * @param {Element | Document | null | string } [scopeOrMessage ] - HTML element `<body>` checked for browser support or support error message
3737 */
38- constructor ( $scope = document . body ) {
39- const supportMessage =
38+ constructor ( scopeOrMessage = document . body ) {
39+ let supportMessage =
4040 'noModule' in HTMLScriptElement . prototype
4141 ? 'GOV.UK Frontend initialised without `<body class="govuk-frontend-supported">` from template `<script>` snippet'
4242 : 'GOV.UK Frontend is not supported in this browser'
4343
44- super (
45- $scope
46- ? supportMessage
47- : 'GOV.UK Frontend initialised without `<script type="module">`'
48- )
44+ if ( ! isScope ( scopeOrMessage ) ) {
45+ supportMessage =
46+ 'GOV.UK Frontend initialised without `<script type="module">`'
47+ }
48+
49+ super ( typeof scopeOrMessage === 'string' ? scopeOrMessage : supportMessage )
4950 }
5051}
5152
You can’t perform that action at this time.
0 commit comments