Skip to content

Commit

Permalink
chore: remove the try/catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Sep 25, 2023
1 parent 797dc1d commit 9a65b06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/graphql/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
26 changes: 9 additions & 17 deletions src/servers/middlewares/idempotency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions test/legacy-integration/services/lock.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -21,7 +21,7 @@ describe("Lock", () => {
})

const result = await Promise.race([lock1, lock2])
expect(result).toBeInstanceOf(ResourceAttemptsLockServiceError)
expect(result).toBeInstanceOf(ResourceAttemptsRedlockServiceError)
})
})
})
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("Redlock", () => {
})
},
}),
).resolves.toBeInstanceOf(ResourceAttemptsLockServiceError)
).resolves.toBeInstanceOf(ResourceAttemptsRedlockServiceError)
})

it("second loop start after first loop has ended", async () => {
Expand Down

0 comments on commit 9a65b06

Please sign in to comment.