Skip to content

The open alternative to Salesforce, designed for AI.

Build lambda error - catch user code compilation errors (#19516)

Thomas Trompette00208dbf1f94
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
index 2a3d5e9f260..668553ab02b 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
@@ -32,6 +32,7 @@ import {
type LogicFunctionTranspileResult,
} from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';
+import { isNonEmptyString } from '@sniptt/guards';
import { ASSET_PATH } from 'src/constants/assets-path';
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { type CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
@@ -529,6 +530,19 @@ export class LambdaDriver implements LogicFunctionDriver {
? JSON.parse(result.Payload.transformToString())
: {};
+ const userCompilationErrorRegex = /^Build failed with \d+ error/;
+
+ const isUserCompilationError =
+ isNonEmptyString(parsedResult?.errorMessage) &&
+ userCompilationErrorRegex.test(parsedResult.errorMessage);
+
+ if (isUserCompilationError) {
+ throw new LogicFunctionException(
+ `Function code compilation failed: ${parsedResult.errorMessage}`,
+ LogicFunctionExceptionCode.LOGIC_FUNCTION_COMPILATION_FAILED,
+ );
+ }
+
throw new LogicFunctionException(
`Builder Lambda failed: ${JSON.stringify(parsedResult)}`,
LogicFunctionExceptionCode.LOGIC_FUNCTION_CREATE_FAILED,
diff --git a/packages/twenty-server/src/engine/metadata-modules/logic-function/logic-function.exception.ts b/packages/twenty-server/src/engine/metadata-modules/logic-function/logic-function.exception.ts
index 326ca7040eb..3741fe0f3f3 100644
--- a/packages/twenty-server/src/engine/metadata-modules/logic-function/logic-function.exception.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/logic-function/logic-function.exception.ts
@@ -12,6 +12,7 @@ export enum LogicFunctionExceptionCode {
LOGIC_FUNCTION_CODE_UNCHANGED = 'LOGIC_FUNCTION_CODE_UNCHANGED',
LOGIC_FUNCTION_EXECUTION_LIMIT_REACHED = 'LOGIC_FUNCTION_EXECUTION_LIMIT_REACHED',
LOGIC_FUNCTION_CREATE_FAILED = 'LOGIC_FUNCTION_CREATE_FAILED',
+ LOGIC_FUNCTION_COMPILATION_FAILED = 'LOGIC_FUNCTION_COMPILATION_FAILED',
LOGIC_FUNCTION_EXECUTION_TIMEOUT = 'LOGIC_FUNCTION_EXECUTION_TIMEOUT',
LOGIC_FUNCTION_DISABLED = 'LOGIC_FUNCTION_DISABLED',
LOGIC_FUNCTION_INVALID_SEED_PROJECT = 'LOGIC_FUNCTION_INVALID_SEED_PROJECT',
@@ -35,6 +36,8 @@ const getLogicFunctionExceptionUserFriendlyMessage = (
return msg`Function execution limit reached.`;
case LogicFunctionExceptionCode.LOGIC_FUNCTION_CREATE_FAILED:
return msg`Failed to create function.`;
+ case LogicFunctionExceptionCode.LOGIC_FUNCTION_COMPILATION_FAILED:
+ return msg`Function code failed to compile.`;
case LogicFunctionExceptionCode.LOGIC_FUNCTION_EXECUTION_TIMEOUT:
return msg`Function execution timed out.`;
case LogicFunctionExceptionCode.LOGIC_FUNCTION_DISABLED:
diff --git a/packages/twenty-server/src/engine/metadata-modules/logic-function/utils/logic-function-graphql-api-exception-handler.utils.ts b/packages/twenty-server/src/engine/metadata-modules/logic-function/utils/logic-function-graphql-api-exception-handler.utils.ts
index e8ca973d184..1e50eeddf05 100644
--- a/packages/twenty-server/src/engine/metadata-modules/logic-function/utils/logic-function-graphql-api-exception-handler.utils.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/logic-function/utils/logic-function-graphql-api-exception-handler.utils.ts
@@ -5,6 +5,7 @@ import {
ForbiddenError,
NotFoundError,
TimeoutError,
+ UserInputError,
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import {
LogicFunctionException,
@@ -29,6 +30,8 @@ export const logicFunctionGraphQLApiExceptionHandler = (error: any) => {
case LogicFunctionExceptionCode.LOGIC_FUNCTION_CREATE_FAILED:
case LogicFunctionExceptionCode.LOGIC_FUNCTION_INVALID_SEED_PROJECT:
throw error;
+ case LogicFunctionExceptionCode.LOGIC_FUNCTION_COMPILATION_FAILED:
+ throw new UserInputError(error);
case LogicFunctionExceptionCode.LOGIC_FUNCTION_DISABLED:
throw new ForbiddenError(error);
default: {