Skip to content

Commit

Permalink
element error refector
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpatrickpatrick committed Sep 18, 2024
1 parent 3a651ba commit 8ac9d02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
24 changes: 24 additions & 0 deletions packages/govuk-frontend/src/govuk/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ function isObject(option) {
return !!option && typeof option === 'object' && !isArray(option)
}

/**
* Format error message
*
* @internal
* @param {CompatibleClass} Component - Component that threw the error
* @param {string} message - Error message
* @returns {string} - Formatted error message
*/
export function formatErrorMessage(Component, message) {
return `${Component.moduleName}:${message}`
}

/**
* Schema for component config
*
Expand Down Expand Up @@ -308,3 +320,15 @@ function isObject(option) {
* @typedef {keyof ObjectNested} NestedKey
* @typedef {{ [key: string]: string | boolean | number | ObjectNested | undefined }} ObjectNested
*/

/* eslint-disable jsdoc/valid-types --
* `{new(...args: any[] ): object}` is not recognised as valid
* https://github.com/gajus/eslint-plugin-jsdoc/issues/145#issuecomment-1308722878
* https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/131
**/

/**
* @typedef {{new (...args: any[]): any, defaults?: object, moduleName: string}} CompatibleClass
*/

/* eslint-enable jsdoc/valid-types */
14 changes: 9 additions & 5 deletions packages/govuk-frontend/src/govuk/errors/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatErrorMessage } from './../common/index.mjs'

/**
* GOV.UK Frontend error
*
Expand Down Expand Up @@ -75,25 +77,25 @@ export class ElementError extends GOVUKFrontendError {
/**
* @internal
* @param {string | ElementErrorOptions} messageOrOptions - Element error message or options
* @param {import('../init.mjs').CompatibleClass} Component - Component that threw Error
*/
constructor(messageOrOptions) {
constructor(messageOrOptions, Component) {
let message = typeof messageOrOptions === 'string' ? messageOrOptions : ''

// Build message from options
if (typeof messageOrOptions === 'object') {
const { componentName, identifier, element, expectedType } =
messageOrOptions
const { identifier, element, expectedType } = messageOrOptions

// Add prefix and identifier
message = `${componentName}: ${identifier}`
message = identifier

// Append reason
message += element
? ` is not of type ${expectedType ?? 'HTMLElement'}`
: ' not found'
}

super(message)
super(formatErrorMessage(Component, message))
}
}

Expand Down Expand Up @@ -129,3 +131,5 @@ export class InitError extends GOVUKFrontendError {
* @property {Element | null} [element] - The element in error
* @property {string} [expectedType] - The type that was expected for the identifier
*/

/** @type {import('./../common/index.mjs').CompatibleClass} */

0 comments on commit 8ac9d02

Please sign in to comment.