@pgpmjs/types 2.13.0 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './error';
2
2
  export * from './error-factory';
3
+ export * from './pg-error-format';
3
4
  export * from './pgpm';
4
5
  export * from './jobs';
5
6
  export * from './update';
@@ -0,0 +1,121 @@
1
+ /**
2
+ * PostgreSQL Error Formatting Utilities
3
+ *
4
+ * Extracts and formats extended PostgreSQL error fields from pg library errors.
5
+ * These fields provide additional context for debugging database errors.
6
+ */
7
+ /**
8
+ * Extract PostgreSQL error fields from an error object.
9
+ * Returns null if the error doesn't appear to be a PostgreSQL error.
10
+ *
11
+ * @param err - The error object to extract fields from
12
+ * @returns PgErrorFields if the error has PG fields, null otherwise
13
+ */
14
+ export function extractPgErrorFields(err) {
15
+ if (!err || typeof err !== 'object') {
16
+ return null;
17
+ }
18
+ const e = err;
19
+ // Check if this looks like a PostgreSQL error (has code or detail)
20
+ if (!e.code && !e.detail && !e.where) {
21
+ return null;
22
+ }
23
+ const fields = {};
24
+ if (typeof e.code === 'string')
25
+ fields.code = e.code;
26
+ if (typeof e.detail === 'string')
27
+ fields.detail = e.detail;
28
+ if (typeof e.hint === 'string')
29
+ fields.hint = e.hint;
30
+ if (typeof e.where === 'string')
31
+ fields.where = e.where;
32
+ if (typeof e.position === 'string')
33
+ fields.position = e.position;
34
+ if (typeof e.internalPosition === 'string')
35
+ fields.internalPosition = e.internalPosition;
36
+ if (typeof e.internalQuery === 'string')
37
+ fields.internalQuery = e.internalQuery;
38
+ if (typeof e.schema === 'string')
39
+ fields.schema = e.schema;
40
+ if (typeof e.table === 'string')
41
+ fields.table = e.table;
42
+ if (typeof e.column === 'string')
43
+ fields.column = e.column;
44
+ if (typeof e.dataType === 'string')
45
+ fields.dataType = e.dataType;
46
+ if (typeof e.constraint === 'string')
47
+ fields.constraint = e.constraint;
48
+ if (typeof e.file === 'string')
49
+ fields.file = e.file;
50
+ if (typeof e.line === 'string')
51
+ fields.line = e.line;
52
+ if (typeof e.routine === 'string')
53
+ fields.routine = e.routine;
54
+ return fields;
55
+ }
56
+ /**
57
+ * Format PostgreSQL error fields into an array of human-readable lines.
58
+ * Only includes fields that are present and non-empty.
59
+ *
60
+ * @param fields - The PostgreSQL error fields to format
61
+ * @returns Array of formatted lines
62
+ */
63
+ export function formatPgErrorFields(fields) {
64
+ const lines = [];
65
+ if (fields.detail)
66
+ lines.push(`Detail: ${fields.detail}`);
67
+ if (fields.hint)
68
+ lines.push(`Hint: ${fields.hint}`);
69
+ if (fields.where)
70
+ lines.push(`Where: ${fields.where}`);
71
+ if (fields.schema)
72
+ lines.push(`Schema: ${fields.schema}`);
73
+ if (fields.table)
74
+ lines.push(`Table: ${fields.table}`);
75
+ if (fields.column)
76
+ lines.push(`Column: ${fields.column}`);
77
+ if (fields.dataType)
78
+ lines.push(`Data Type: ${fields.dataType}`);
79
+ if (fields.constraint)
80
+ lines.push(`Constraint: ${fields.constraint}`);
81
+ if (fields.position)
82
+ lines.push(`Position: ${fields.position}`);
83
+ if (fields.internalQuery)
84
+ lines.push(`Internal Query: ${fields.internalQuery}`);
85
+ if (fields.internalPosition)
86
+ lines.push(`Internal Position: ${fields.internalPosition}`);
87
+ return lines;
88
+ }
89
+ /**
90
+ * Format a PostgreSQL error with full context for debugging.
91
+ * Combines the original error message with extended PostgreSQL fields
92
+ * and optional query context.
93
+ *
94
+ * @param err - The error object
95
+ * @param context - Optional query context (SQL and parameters)
96
+ * @returns Formatted error string with all available information
97
+ */
98
+ export function formatPgError(err, context) {
99
+ if (!err || typeof err !== 'object') {
100
+ return String(err);
101
+ }
102
+ const e = err;
103
+ const message = typeof e.message === 'string' ? e.message : String(err);
104
+ const lines = [message];
105
+ // Add PostgreSQL error fields
106
+ const pgFields = extractPgErrorFields(err);
107
+ if (pgFields) {
108
+ const fieldLines = formatPgErrorFields(pgFields);
109
+ if (fieldLines.length > 0) {
110
+ lines.push(...fieldLines);
111
+ }
112
+ }
113
+ // Add query context
114
+ if (context?.query) {
115
+ lines.push(`Query: ${context.query}`);
116
+ }
117
+ if (context?.values !== undefined) {
118
+ lines.push(`Values: ${JSON.stringify(context.values)}`);
119
+ }
120
+ return lines.join('\n');
121
+ }
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './error';
2
2
  export * from './error-factory';
3
+ export * from './pg-error-format';
3
4
  export * from './pgpm';
4
5
  export * from './jobs';
5
6
  export * from './update';
package/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./error"), exports);
18
18
  __exportStar(require("./error-factory"), exports);
19
+ __exportStar(require("./pg-error-format"), exports);
19
20
  __exportStar(require("./pgpm"), exports);
20
21
  __exportStar(require("./jobs"), exports);
21
22
  __exportStar(require("./update"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgpmjs/types",
3
- "version": "2.13.0",
3
+ "version": "2.14.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PGPM types",
6
6
  "main": "index.js",
@@ -29,7 +29,7 @@
29
29
  "test:watch": "jest --watch"
30
30
  },
31
31
  "dependencies": {
32
- "pg-env": "^1.2.4"
32
+ "pg-env": "^1.2.5"
33
33
  },
34
34
  "keywords": [
35
35
  "types",
@@ -40,7 +40,7 @@
40
40
  "migrations"
41
41
  ],
42
42
  "devDependencies": {
43
- "makage": "^0.1.9"
43
+ "makage": "^0.1.10"
44
44
  },
45
- "gitHead": "f04912cac2edf6435c6add5a6ec65a11d6b79df2"
45
+ "gitHead": "f2f9c9851beff3214790dfca371e4ca7f6c1373f"
46
46
  }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * PostgreSQL Error Formatting Utilities
3
+ *
4
+ * Extracts and formats extended PostgreSQL error fields from pg library errors.
5
+ * These fields provide additional context for debugging database errors.
6
+ */
7
+ /**
8
+ * Extended PostgreSQL error fields available from pg-protocol.
9
+ * These fields are populated by PostgreSQL when an error occurs.
10
+ */
11
+ export interface PgErrorFields {
12
+ /** PostgreSQL error code (e.g., '42P01' for undefined table) */
13
+ code?: string;
14
+ /** Additional detail about the error */
15
+ detail?: string;
16
+ /** Suggestion for fixing the error */
17
+ hint?: string;
18
+ /** PL/pgSQL call stack or context */
19
+ where?: string;
20
+ /** Character position in the query where the error occurred */
21
+ position?: string;
22
+ /** Position in internal query */
23
+ internalPosition?: string;
24
+ /** Internal query that caused the error */
25
+ internalQuery?: string;
26
+ /** Schema name related to the error */
27
+ schema?: string;
28
+ /** Table name related to the error */
29
+ table?: string;
30
+ /** Column name related to the error */
31
+ column?: string;
32
+ /** Data type name related to the error */
33
+ dataType?: string;
34
+ /** Constraint name related to the error */
35
+ constraint?: string;
36
+ /** Source file in PostgreSQL where error was generated */
37
+ file?: string;
38
+ /** Line number in PostgreSQL source file */
39
+ line?: string;
40
+ /** PostgreSQL routine that generated the error */
41
+ routine?: string;
42
+ }
43
+ /**
44
+ * Context about the query that caused the error.
45
+ */
46
+ export interface PgErrorContext {
47
+ /** The SQL query that was executed */
48
+ query?: string;
49
+ /** Parameter values passed to the query */
50
+ values?: any[];
51
+ }
52
+ /**
53
+ * Extract PostgreSQL error fields from an error object.
54
+ * Returns null if the error doesn't appear to be a PostgreSQL error.
55
+ *
56
+ * @param err - The error object to extract fields from
57
+ * @returns PgErrorFields if the error has PG fields, null otherwise
58
+ */
59
+ export declare function extractPgErrorFields(err: unknown): PgErrorFields | null;
60
+ /**
61
+ * Format PostgreSQL error fields into an array of human-readable lines.
62
+ * Only includes fields that are present and non-empty.
63
+ *
64
+ * @param fields - The PostgreSQL error fields to format
65
+ * @returns Array of formatted lines
66
+ */
67
+ export declare function formatPgErrorFields(fields: PgErrorFields): string[];
68
+ /**
69
+ * Format a PostgreSQL error with full context for debugging.
70
+ * Combines the original error message with extended PostgreSQL fields
71
+ * and optional query context.
72
+ *
73
+ * @param err - The error object
74
+ * @param context - Optional query context (SQL and parameters)
75
+ * @returns Formatted error string with all available information
76
+ */
77
+ export declare function formatPgError(err: unknown, context?: PgErrorContext): string;
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ /**
3
+ * PostgreSQL Error Formatting Utilities
4
+ *
5
+ * Extracts and formats extended PostgreSQL error fields from pg library errors.
6
+ * These fields provide additional context for debugging database errors.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.extractPgErrorFields = extractPgErrorFields;
10
+ exports.formatPgErrorFields = formatPgErrorFields;
11
+ exports.formatPgError = formatPgError;
12
+ /**
13
+ * Extract PostgreSQL error fields from an error object.
14
+ * Returns null if the error doesn't appear to be a PostgreSQL error.
15
+ *
16
+ * @param err - The error object to extract fields from
17
+ * @returns PgErrorFields if the error has PG fields, null otherwise
18
+ */
19
+ function extractPgErrorFields(err) {
20
+ if (!err || typeof err !== 'object') {
21
+ return null;
22
+ }
23
+ const e = err;
24
+ // Check if this looks like a PostgreSQL error (has code or detail)
25
+ if (!e.code && !e.detail && !e.where) {
26
+ return null;
27
+ }
28
+ const fields = {};
29
+ if (typeof e.code === 'string')
30
+ fields.code = e.code;
31
+ if (typeof e.detail === 'string')
32
+ fields.detail = e.detail;
33
+ if (typeof e.hint === 'string')
34
+ fields.hint = e.hint;
35
+ if (typeof e.where === 'string')
36
+ fields.where = e.where;
37
+ if (typeof e.position === 'string')
38
+ fields.position = e.position;
39
+ if (typeof e.internalPosition === 'string')
40
+ fields.internalPosition = e.internalPosition;
41
+ if (typeof e.internalQuery === 'string')
42
+ fields.internalQuery = e.internalQuery;
43
+ if (typeof e.schema === 'string')
44
+ fields.schema = e.schema;
45
+ if (typeof e.table === 'string')
46
+ fields.table = e.table;
47
+ if (typeof e.column === 'string')
48
+ fields.column = e.column;
49
+ if (typeof e.dataType === 'string')
50
+ fields.dataType = e.dataType;
51
+ if (typeof e.constraint === 'string')
52
+ fields.constraint = e.constraint;
53
+ if (typeof e.file === 'string')
54
+ fields.file = e.file;
55
+ if (typeof e.line === 'string')
56
+ fields.line = e.line;
57
+ if (typeof e.routine === 'string')
58
+ fields.routine = e.routine;
59
+ return fields;
60
+ }
61
+ /**
62
+ * Format PostgreSQL error fields into an array of human-readable lines.
63
+ * Only includes fields that are present and non-empty.
64
+ *
65
+ * @param fields - The PostgreSQL error fields to format
66
+ * @returns Array of formatted lines
67
+ */
68
+ function formatPgErrorFields(fields) {
69
+ const lines = [];
70
+ if (fields.detail)
71
+ lines.push(`Detail: ${fields.detail}`);
72
+ if (fields.hint)
73
+ lines.push(`Hint: ${fields.hint}`);
74
+ if (fields.where)
75
+ lines.push(`Where: ${fields.where}`);
76
+ if (fields.schema)
77
+ lines.push(`Schema: ${fields.schema}`);
78
+ if (fields.table)
79
+ lines.push(`Table: ${fields.table}`);
80
+ if (fields.column)
81
+ lines.push(`Column: ${fields.column}`);
82
+ if (fields.dataType)
83
+ lines.push(`Data Type: ${fields.dataType}`);
84
+ if (fields.constraint)
85
+ lines.push(`Constraint: ${fields.constraint}`);
86
+ if (fields.position)
87
+ lines.push(`Position: ${fields.position}`);
88
+ if (fields.internalQuery)
89
+ lines.push(`Internal Query: ${fields.internalQuery}`);
90
+ if (fields.internalPosition)
91
+ lines.push(`Internal Position: ${fields.internalPosition}`);
92
+ return lines;
93
+ }
94
+ /**
95
+ * Format a PostgreSQL error with full context for debugging.
96
+ * Combines the original error message with extended PostgreSQL fields
97
+ * and optional query context.
98
+ *
99
+ * @param err - The error object
100
+ * @param context - Optional query context (SQL and parameters)
101
+ * @returns Formatted error string with all available information
102
+ */
103
+ function formatPgError(err, context) {
104
+ if (!err || typeof err !== 'object') {
105
+ return String(err);
106
+ }
107
+ const e = err;
108
+ const message = typeof e.message === 'string' ? e.message : String(err);
109
+ const lines = [message];
110
+ // Add PostgreSQL error fields
111
+ const pgFields = extractPgErrorFields(err);
112
+ if (pgFields) {
113
+ const fieldLines = formatPgErrorFields(pgFields);
114
+ if (fieldLines.length > 0) {
115
+ lines.push(...fieldLines);
116
+ }
117
+ }
118
+ // Add query context
119
+ if (context?.query) {
120
+ lines.push(`Query: ${context.query}`);
121
+ }
122
+ if (context?.values !== undefined) {
123
+ lines.push(`Values: ${JSON.stringify(context.values)}`);
124
+ }
125
+ return lines.join('\n');
126
+ }