-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows components to indicate they didn't inistantiate because GOV.UK Frontend is not supported
- Loading branch information
1 parent
2e57cd6
commit a533c7e
Showing
7 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* A base class for `Error`s thrown by GOV.UK Frontend. | ||
* | ||
* It is meant to be extended into specific types of errors | ||
* to be thrown by our code. | ||
* | ||
* @example | ||
* ```js | ||
* class MissingRootError extends GOVUKFrontendError { | ||
* // Setting an explicit name is important as extending the class will not | ||
* // set a new `name` on the subclass. The `name` property is important | ||
* // to ensure intelligible error names even if the class name gets | ||
* // mangled by a minifier | ||
* name = "MissingRootError" | ||
* } | ||
* ``` | ||
* @abstract | ||
*/ | ||
export class GOVUKFrontendError extends Error { | ||
name = 'GOVUKFrontendError' | ||
} | ||
|
||
/** | ||
* Indicates that GOV.UK Frontend is not supported | ||
*/ | ||
export class GOVUKFrontendNotSupportedError extends GOVUKFrontendError { | ||
name = 'GOVUKFrontendNotSupportedError' | ||
|
||
/** */ | ||
constructor () { | ||
super('GOV.UK Frontend is not supported in this browser') | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/govuk-frontend/src/govuk/errors/index.unit.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { GOVUKFrontendError, GOVUKFrontendNotSupportedError } from './index.mjs' | ||
|
||
describe('errors', () => { | ||
describe('GOVUKFrontendError', () => { | ||
it('allows subclasses to set a custom name', () => { | ||
class CustomError extends GOVUKFrontendError { | ||
name = 'CustomName' | ||
} | ||
|
||
expect(new CustomError().name).toBe('CustomName') | ||
}) | ||
}) | ||
|
||
describe('GOVUKFrontendNotSupportedError', () => { | ||
it('is an instance of GOVUKFrontendError', () => { | ||
expect(new GOVUKFrontendNotSupportedError()).toBeInstanceOf( | ||
GOVUKFrontendError | ||
) | ||
}) | ||
it('has its own name set', () => { | ||
expect(new GOVUKFrontendNotSupportedError().name).toBe( | ||
'GOVUKFrontendNotSupportedError' | ||
) | ||
}) | ||
it('provides meaningfull feedback to users', () => { | ||
expect(new GOVUKFrontendNotSupportedError().message).toBe( | ||
'GOV.UK Frontend is not supported in this browser' | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters