From 0e24b42b2d1b30b722d124e6ab953b8efd19a9ae Mon Sep 17 00:00:00 2001 From: Nicolas Burtey Date: Mon, 25 Sep 2023 11:33:23 +0100 Subject: [PATCH] chore: setting ResourceAttemptsTimelockServiceError to warn instead of critical --- src/domain/lock/errors.ts | 5 ++++- src/domain/lock/index.types.d.ts | 4 +--- src/servers/middlewares/idempotency.ts | 4 ++-- src/services/lock/index.ts | 17 +++++++++++++---- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/domain/lock/errors.ts b/src/domain/lock/errors.ts index c8418ccf52d..621cb15d19e 100644 --- a/src/domain/lock/errors.ts +++ b/src/domain/lock/errors.ts @@ -3,7 +3,10 @@ import { DomainError, ErrorLevel } from "@domain/shared" export class LockError extends DomainError {} export class LockServiceError extends LockError {} -export class ResourceAttemptsLockServiceError extends LockServiceError { +export class ResourceAttemptsRedlockServiceError extends LockServiceError { + level = ErrorLevel.Warn +} +export class ResourceAttemptsTimelockServiceError extends LockServiceError { level = ErrorLevel.Warn } export class ResourceExpiredLockServiceError extends LockServiceError { diff --git a/src/domain/lock/index.types.d.ts b/src/domain/lock/index.types.d.ts index 720db6a8d39..8006227874d 100644 --- a/src/domain/lock/index.types.d.ts +++ b/src/domain/lock/index.types.d.ts @@ -1,5 +1,3 @@ -type ExecutionError = import("redlock").ExecutionError - type RedlockAbortSignal = import("redlock").RedlockAbortSignal type LockServiceError = import("./errors").LockServiceError @@ -26,7 +24,7 @@ interface ILockService { { txHash, vout }: { txHash: OnChainTxHash; vout: OnChainTxVout }, f: (signal: OnChainTxAbortSignal) => Promise, ): Promise - lockIdempotencyKey(idempotencyKey: IdempotencyKey): Promise + lockIdempotencyKey(idempotencyKey: IdempotencyKey): Promise } type RedlockArgs = { diff --git a/src/servers/middlewares/idempotency.ts b/src/servers/middlewares/idempotency.ts index d16443b0314..d453b6b66c6 100644 --- a/src/servers/middlewares/idempotency.ts +++ b/src/servers/middlewares/idempotency.ts @@ -1,13 +1,13 @@ import { NextFunction, Request, Response } from "express" import { LockService } from "@services/lock" import { InvalidIdempotencyKeyError } from "@domain/errors" -import { ExecutionError } from "redlock" import { addAttributesToCurrentSpan, recordExceptionInCurrentSpan, } from "@services/tracing" import { ErrorLevel } from "@domain/shared" import { json } from "body-parser" +import { ResourceAttemptsTimelockServiceError } from "@domain/lock" // Create lock service instance const lockService = LockService() @@ -81,7 +81,7 @@ export const idempotencyMiddleware = async ( fallbackMsg: "Error locking idempotency key", level: ErrorLevel.Critical, }) - if (error instanceof ExecutionError) { + if (error instanceof ResourceAttemptsTimelockServiceError) { return res.status(409).json({ error: "the idempotency key already exist" }) } if (error instanceof Error) { diff --git a/src/services/lock/index.ts b/src/services/lock/index.ts index 9d7acea3447..185f127874b 100644 --- a/src/services/lock/index.ts +++ b/src/services/lock/index.ts @@ -3,7 +3,8 @@ import Redlock, { ExecutionError } from "redlock" import { NETWORK } from "@config" import { - ResourceAttemptsLockServiceError, + ResourceAttemptsRedlockServiceError, + ResourceAttemptsTimelockServiceError, ResourceExpiredLockServiceError, UnknownLockServiceError, } from "@domain/lock" @@ -93,7 +94,7 @@ export const redlock = async ({ ) } catch (error) { if (error instanceof ExecutionError) { - return new ResourceAttemptsLockServiceError() + return new ResourceAttemptsRedlockServiceError() } return new UnknownLockServiceError(parseErrorMessageFromUnknown(error)) @@ -152,10 +153,18 @@ export const LockService = (): ILockService => { const lockIdempotencyKey = async ( idempotencyKey: IdempotencyKey, - ): Promise => { + ): Promise => { const path = getIdempotencyKeyLockResource(idempotencyKey) - await timelock({ resource: path, duration: durationLockIdempotencyKey }) + try { + await timelock({ resource: path, duration: durationLockIdempotencyKey }) + } catch (err) { + if (err instanceof ExecutionError) { + return new ResourceAttemptsTimelockServiceError() + } + + return new UnknownLockServiceError(err) + } } return wrapAsyncFunctionsToRunInSpan({