Skip to content

Commit 1ba00b1

Browse files
committed
Added isPrismaRetriableError()
1 parent 3ec6983 commit 1ba00b1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

internal-packages/database/src/transaction.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ function isTransactionClient(prisma: PrismaClientOrTransaction): prisma is Prism
1313
return !("$transaction" in prisma);
1414
}
1515

16-
function isPrismaKnownError(error: unknown): error is Prisma.PrismaClientKnownRequestError {
16+
export function isPrismaKnownError(error: unknown): error is Prisma.PrismaClientKnownRequestError {
1717
return (
1818
typeof error === "object" && error !== null && "code" in error && typeof error.code === "string"
1919
);
2020
}
2121

22+
export function isPrismaRetriableError(error: unknown): boolean {
23+
if (!isPrismaKnownError(error)) {
24+
return false;
25+
}
26+
27+
return retryCodes.includes(error.code);
28+
}
29+
2230
export type PrismaTransactionOptions = {
2331
/** The maximum amount of time (in ms) Prisma Client will wait to acquire a transaction from the database. The default value is 2000ms. */
2432
maxWait?: number;
@@ -39,6 +47,9 @@ export type PrismaTransactionOptions = {
3947
maxRetries?: number;
4048
};
4149

50+
//retry these Prisma error codes
51+
const retryCodes = ["P2034", "P2028"];
52+
4253
export async function $transaction<R>(
4354
prisma: PrismaClientOrTransaction,
4455
fn: (prisma: PrismaTransactionClient) => Promise<R>,
@@ -55,7 +66,7 @@ export async function $transaction<R>(
5566
} catch (error) {
5667
if (isPrismaKnownError(error)) {
5768
if (
58-
error.code === "P2034" &&
69+
retryCodes.includes(error.code) &&
5970
typeof options?.maxRetries === "number" &&
6071
attempt < options.maxRetries
6172
) {

0 commit comments

Comments
 (0)