Skip to content

Commit

Permalink
chore: setting ResourceAttemptsTimelockServiceError to warn instead o…
Browse files Browse the repository at this point in the history
…f critical
  • Loading branch information
Nicolas Burtey committed Sep 25, 2023
1 parent 813bb28 commit 0e24b42
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/domain/lock/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/domain/lock/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type ExecutionError = import("redlock").ExecutionError

type RedlockAbortSignal = import("redlock").RedlockAbortSignal

type LockServiceError = import("./errors").LockServiceError
Expand All @@ -26,7 +24,7 @@ interface ILockService {
{ txHash, vout }: { txHash: OnChainTxHash; vout: OnChainTxVout },
f: (signal: OnChainTxAbortSignal) => Promise<Res>,
): Promise<Res | LockServiceError>
lockIdempotencyKey(idempotencyKey: IdempotencyKey): Promise<void | ExecutionError>
lockIdempotencyKey(idempotencyKey: IdempotencyKey): Promise<void | LockServiceError>
}

type RedlockArgs<Signal, Ret> = {
Expand Down
4 changes: 2 additions & 2 deletions src/servers/middlewares/idempotency.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions src/services/lock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Redlock, { ExecutionError } from "redlock"
import { NETWORK } from "@config"

import {
ResourceAttemptsLockServiceError,
ResourceAttemptsRedlockServiceError,
ResourceAttemptsTimelockServiceError,
ResourceExpiredLockServiceError,
UnknownLockServiceError,
} from "@domain/lock"
Expand Down Expand Up @@ -93,7 +94,7 @@ export const redlock = async <Signal extends RedlockAbortSignal, Ret>({
)
} catch (error) {
if (error instanceof ExecutionError) {
return new ResourceAttemptsLockServiceError()
return new ResourceAttemptsRedlockServiceError()
}

return new UnknownLockServiceError(parseErrorMessageFromUnknown(error))
Expand Down Expand Up @@ -152,10 +153,18 @@ export const LockService = (): ILockService => {

const lockIdempotencyKey = async (
idempotencyKey: IdempotencyKey,
): Promise<void | ExecutionError> => {
): Promise<void | LockServiceError> => {
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({
Expand Down

0 comments on commit 0e24b42

Please sign in to comment.