diff --git a/packages/twenty-server/src/engine/core-modules/cache-lock/cache-lock.service.ts b/packages/twenty-server/src/engine/core-modules/cache-lock/cache-lock.service.ts index 930d9c50d0d..e7c20f5916a 100644 --- a/packages/twenty-server/src/engine/core-modules/cache-lock/cache-lock.service.ts +++ b/packages/twenty-server/src/engine/core-modules/cache-lock/cache-lock.service.ts @@ -1,5 +1,9 @@ import { Injectable, Logger } from '@nestjs/common'; +import { + CacheLockException, + CacheLockExceptionCode, +} from 'src/engine/core-modules/cache-lock/exceptions/cache-lock.exception'; import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator'; import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum'; import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service'; @@ -10,13 +14,6 @@ export type CacheLockOptions = { ttl?: number; }; -export class CacheLockAcquisitionError extends Error { - constructor(key: string) { - super(`Failed to acquire lock for key: ${key}`); - this.name = 'CacheLockAcquisitionError'; - } -} - @Injectable() export class CacheLockService { private readonly logger = new Logger(CacheLockService.name); @@ -57,6 +54,9 @@ export class CacheLockService { await this.delay(ms); } - throw new CacheLockAcquisitionError(key); + throw new CacheLockException( + `Failed to acquire lock for key: ${key}`, + CacheLockExceptionCode.LOCK_ACQUISITION_TIMEOUT, + ); } } diff --git a/packages/twenty-server/src/engine/core-modules/cache-lock/exceptions/cache-lock.exception.ts b/packages/twenty-server/src/engine/core-modules/cache-lock/exceptions/cache-lock.exception.ts new file mode 100644 index 00000000000..d00417b4ef3 --- /dev/null +++ b/packages/twenty-server/src/engine/core-modules/cache-lock/exceptions/cache-lock.exception.ts @@ -0,0 +1,26 @@ +import { type MessageDescriptor } from '@lingui/core'; +import { msg } from '@lingui/core/macro'; + +import { + appendCommonExceptionCode, + CustomException, +} from 'src/utils/custom-exception'; + +export class CacheLockException extends CustomException< + keyof typeof CacheLockExceptionCode +> { + constructor( + message: string, + code: keyof typeof CacheLockExceptionCode, + { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {}, + ) { + super(message, code, { + userFriendlyMessage: + userFriendlyMessage ?? msg`A cache lock error occurred.`, + }); + } +} + +export const CacheLockExceptionCode = appendCommonExceptionCode({ + LOCK_ACQUISITION_TIMEOUT: 'LOCK_ACQUISITION_TIMEOUT', +} as const); diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts index 45d1f86a3c0..be7a5f9d8cf 100644 --- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts +++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts @@ -17,10 +17,11 @@ import { isNonEmptyString } from '@sniptt/guards'; import { isDefined } from 'twenty-shared/utils'; 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'; import { - CacheLockAcquisitionError, - type CacheLockService, -} from 'src/engine/core-modules/cache-lock/cache-lock.service'; + CacheLockException, + CacheLockExceptionCode, +} from 'src/engine/core-modules/cache-lock/exceptions/cache-lock.exception'; import { EXECUTOR_LAMBDA_MEMORY_MB, EXECUTOR_LAMBDA_TIMEOUT_SECONDS, @@ -133,7 +134,11 @@ export class LambdaExecutorManagerService { }, ); } catch (error) { - if (!(error instanceof CacheLockAcquisitionError)) { + const isLockAcquisitionTimeout = + error instanceof CacheLockException && + error.code === CacheLockExceptionCode.LOCK_ACQUISITION_TIMEOUT; + + if (!isLockAcquisitionTimeout) { throw error; }