Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/server/plugins/engine/components/helpers/components.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ComponentType, type ComponentDef } from '@defra/forms-model'

import { config } from '~/src/config/index.js'
import { createLogger } from '~/src/server/common/helpers/logging/logger.js'
import { type ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'
import { ListFormComponent } from '~/src/server/plugins/engine/components/ListFormComponent.js'
import { escapeMarkdown } from '~/src/server/plugins/engine/components/helpers/index.js'
import * as Components from '~/src/server/plugins/engine/components/index.js'
import { markdown } from '~/src/server/plugins/engine/components/markdownParser.js'
import { type FormState } from '~/src/server/plugins/engine/types.js'

const logger = createLogger()

// All component instances
export type Component = InstanceType<
(typeof Components)[keyof typeof Components]
Expand Down Expand Up @@ -191,10 +194,11 @@ export function createComponent(
case ComponentType.HiddenField:
component = new Components.HiddenField(def, options)
break
}

if (typeof component === 'undefined') {
throw new Error(`Component type ${def.type} does not exist`)
default:
// @ts-expect-error - may be a type that isn't in ComponentType
logger.error(`Component type ${def.type} does not exist`)
component = new Components.TextField(def, options)
Copy link
Copy Markdown
Contributor

@alexluckett alexluckett Dec 3, 2025

Choose a reason for hiding this comment

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

I'm not sure this is the right approach - it could result in components silently falling back on Text Fields if people aren't proactively monitoring logs.

As far as Designer goes, this could be viable style of approach. But the plugin shouldn't be capturing data if the component isn't valid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok. This call is used by the preview in designer so blows up if the component does not exist. I'll try to handle this silently in designer so we keep the runner able to throw the error here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Closing this as the designer now handles an invalid component

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you

}

return component
Expand Down
28 changes: 17 additions & 11 deletions src/server/plugins/engine/components/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HiddenField } from '~/src/server/plugins/engine/components/HiddenField.
import { LatLongField } from '~/src/server/plugins/engine/components/LatLongField.js'
import { NationalGridFieldNumberField } from '~/src/server/plugins/engine/components/NationalGridFieldNumberField.js'
import { OsGridRefField } from '~/src/server/plugins/engine/components/OsGridRefField.js'
import { TextField } from '~/src/server/plugins/engine/components/TextField.js'
import { createComponent } from '~/src/server/plugins/engine/components/helpers/components.js'
import {
createLowerFirstExpression,
Expand All @@ -19,17 +20,22 @@ const formModel = new FormModel(definition, {
})

describe('helpers tests', () => {
test('should throw if invalid type', () => {
expect(() =>
createComponent(
{
type: 'invalid-type'
} as unknown as ComponentDef,
{
model: formModel
}
)
).toThrow('Component type invalid-type does not exist')
test('should default if invalid type', () => {
const component = createComponent(
{
type: 'invalid-type',
name: 'InvalidField',
shortDescription: 'Short desc',
title: 'My invalid field',
options: {}
} as unknown as ComponentDef,
{
model: formModel
}
)
expect(component).toBeInstanceOf(TextField)
expect(component.name).toBe('InvalidField')
expect(component.title).toBe('My invalid field')
})

test('should create EastingNorthingField component', () => {
Expand Down
Loading