refactor(server): move cache lock exception to dedicated exception file

This commit is contained in:
Thomas Trompette
2026-06-11 12:03:37 +02:00
parent 04ceb13435
commit 09d216ec8a
3 changed files with 43 additions and 12 deletions

View File

@@ -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,
);
}
}

View File

@@ -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);

View File

@@ -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;
}