From 9a65b062665e87c7e310cf35a89048b531ff0505 Mon Sep 17 00:00:00 2001 From: Nicolas Burtey Date: Mon, 25 Sep 2023 12:00:25 +0100 Subject: [PATCH] chore: remove the try/catch block --- src/graphql/error-map.ts | 3 ++- src/servers/middlewares/idempotency.ts | 26 +++++++------------ test/legacy-integration/services/lock.spec.ts | 6 ++--- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/graphql/error-map.ts b/src/graphql/error-map.ts index bf3aade1238..b0bb2bc099a 100644 --- a/src/graphql/error-map.ts +++ b/src/graphql/error-map.ts @@ -499,7 +499,8 @@ export const mapError = (error: ApplicationError): CustomApolloError => { case "CouldNotFindLnPaymentFromHashError": case "LockError": case "LockServiceError": - case "ResourceAttemptsLockServiceError": + case "ResourceAttemptsRedlockServiceError": + case "ResourceAttemptsTimelockServiceError": case "ResourceExpiredLockServiceError": case "PriceError": case "PriceServiceError": diff --git a/src/servers/middlewares/idempotency.ts b/src/servers/middlewares/idempotency.ts index d453b6b66c6..e907c002a90 100644 --- a/src/servers/middlewares/idempotency.ts +++ b/src/servers/middlewares/idempotency.ts @@ -70,25 +70,17 @@ export const idempotencyMiddleware = async ( const idempotencyKeyMaybeSuffix = (idempotencyKey + (isPersistedQueryWithExtension ? "-persisted" : "")) as IdempotencyKey - try { - await lockService.lockIdempotencyKey(idempotencyKeyMaybeSuffix) - addAttributesToCurrentSpan({ idempotencyKey }) + const result = await lockService.lockIdempotencyKey(idempotencyKeyMaybeSuffix) + addAttributesToCurrentSpan({ idempotencyKey }) - next() - } catch (error) { - recordExceptionInCurrentSpan({ - error, - fallbackMsg: "Error locking idempotency key", - level: ErrorLevel.Critical, - }) - if (error instanceof ResourceAttemptsTimelockServiceError) { - return res.status(409).json({ error: "the idempotency key already exist" }) - } - if (error instanceof Error) { - return res.status(500).json({ error: error.message }) - } - return res.status(500).json({ error: "Unknown error in idempotency middleware" }) + if (result instanceof ResourceAttemptsTimelockServiceError) { + return res.status(409).json({ error: "the idempotency key already exist" }) } + if (result instanceof Error) { + return res.status(500).json({ error: result.message }) + } + + next() }) } else { next() diff --git a/test/legacy-integration/services/lock.spec.ts b/test/legacy-integration/services/lock.spec.ts index 5e73846d7e4..ebff9af59c8 100644 --- a/test/legacy-integration/services/lock.spec.ts +++ b/test/legacy-integration/services/lock.spec.ts @@ -1,9 +1,9 @@ import { sleep } from "@utils" import { LockService, redlock } from "@services/lock" -import { ResourceAttemptsLockServiceError } from "@domain/lock" import { redis } from "@services/redis" import { baseLogger } from "@services/logger" +import { ResourceAttemptsRedlockServiceError } from "@domain/lock" describe("Lock", () => { describe("lockWalletId", () => { @@ -21,7 +21,7 @@ describe("Lock", () => { }) const result = await Promise.race([lock1, lock2]) - expect(result).toBeInstanceOf(ResourceAttemptsLockServiceError) + expect(result).toBeInstanceOf(ResourceAttemptsRedlockServiceError) }) }) }) @@ -77,7 +77,7 @@ describe("Redlock", () => { }) }, }), - ).resolves.toBeInstanceOf(ResourceAttemptsLockServiceError) + ).resolves.toBeInstanceOf(ResourceAttemptsRedlockServiceError) }) it("second loop start after first loop has ended", async () => {