How to handle Postgres errors inside try catch (with Drizzle)? #916
Replies: 3 comments
-
Does anyone know ? I'm stumbling upon the same situation right now. |
Beta Was this translation helpful? Give feedback.
-
I noticed that the error message comes from the database library I use in drizzle. In my case, it is "pg". |
Beta Was this translation helpful? Give feedback.
-
Is this concept possible in the first place? I'm running a try catch inside a transaction to watch for a db conflict error, but the entire transaction is throwing this error while the closure in the transaction keeps on running. For example: await db.transaction(async (tx) => {
let retryCount = 0;
while (retryCount < 10) {
try {
await tx.insert()
} catch (error) {
const queryError = error as QueryError;
if (
typeof queryError.code === "string" &&
queryError.code === "23505"
) {
// Unique violation (Postgres specific error code)
// Regenerate slug by appending a unique identifier (e.g., retryCount)
slug = slugify(knowledge.title) + "-" + (retryCount + 1);
retryCount++;
} else {
throw error; // Re-throw other errors
}
}
}
} But once the first error is thrown, the entire transaction seems to return, while the code just continues to execute. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to understand how to properly type my errors when Drizzle throws due to a Postgres error.
Example, when I attempt to insert a number into a string column, I was thinking within my catch block I could use:
Should I just use PostgresError?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions