From 1f8e1a522dcdfa4d07ab38b72ac6d4c37bc4f034 Mon Sep 17 00:00:00 2001 From: Iain Sproat <68657+iainsproat@users.noreply.github.com> Date: Thu, 29 May 2025 10:08:43 +0100 Subject: [PATCH] fix(automate): better handle trigger definition errors (#4845) --- .../automate/services/automationManagement.ts | 25 ++++++++++++++++--- .../automate/tests/automations.spec.ts | 6 ++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/server/modules/automate/services/automationManagement.ts b/packages/server/modules/automate/services/automationManagement.ts index 23f7cd223..4b8d761e9 100644 --- a/packages/server/modules/automate/services/automationManagement.ts +++ b/packages/server/modules/automate/services/automationManagement.ts @@ -10,7 +10,12 @@ import { getFunctionReleaseFactory, getFunctionReleasesFactory } from '@/modules/automate/clients/executionEngine' -import { Automate, Roles, removeNullOrUndefinedKeys } from '@speckle/shared' +import { + Automate, + Roles, + ensureError, + removeNullOrUndefinedKeys +} from '@speckle/shared' import { AuthCodePayloadAction } from '@/modules/automate/services/authCode' import { ProjectAutomationCreateInput, @@ -52,6 +57,7 @@ import { GetBranchesByIds } from '@/modules/core/domain/branches/operations' import { ValidateStreamAccess } from '@/modules/core/domain/streams/operations' import { EventBusEmit } from '@/modules/shared/services/eventBus' import { AutomationEvents } from '@/modules/automate/domain/events' +import { UnformattableTriggerDefinitionSchemaError } from '@speckle/shared/dist/commonjs/automate/index.js' export type CreateAutomationDeps = { createAuthCode: CreateStoredAuthCode @@ -395,9 +401,20 @@ export const createAutomationRevisionFactory = userResourceAccessRules ) - const triggers = Automate.AutomateTypes.formatTriggerDefinitionSchema( - input.triggerDefinitions - ) + let triggers: Automate.AutomateTypes.TriggerDefinitionsSchema + try { + triggers = Automate.AutomateTypes.formatTriggerDefinitionSchema( + input.triggerDefinitions + ) + } catch (e) { + if (e instanceof UnformattableTriggerDefinitionSchemaError) { + throw new AutomationRevisionCreationError( + 'One or more trigger definitions are not valid', + { cause: ensureError(e, 'Unknown error when formatting trigger definition') } + ) + } + throw e + } const triggerDefinitions = triggers.definitions.map((d) => { if (Automate.AutomateTypes.isVersionCreatedTriggerDefinition(d)) { const triggerDef: InsertableAutomationRevisionTrigger = { diff --git a/packages/server/modules/automate/tests/automations.spec.ts b/packages/server/modules/automate/tests/automations.spec.ts index 6122aaaab..34537636a 100644 --- a/packages/server/modules/automate/tests/automations.spec.ts +++ b/packages/server/modules/automate/tests/automations.spec.ts @@ -1,5 +1,6 @@ import { AutomationCreationError, + AutomationRevisionCreationError, AutomationUpdateError } from '@/modules/automate/errors/management' import { @@ -406,10 +407,7 @@ const buildAutomationUpdate = () => { }) ) - expect( - e instanceof Automate.UnformattableTriggerDefinitionSchemaError, - e.toString() - ).to.be.true + expect(e instanceof AutomationRevisionCreationError, e.toString()).to.be.true }) })