Skip to content

Commit dc20805

Browse files
committed
feat: add more error codes
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent 0996799 commit dc20805

2 files changed

Lines changed: 94 additions & 32 deletions

File tree

‎lib/normalize-error.js‎

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,56 @@ module.exports = function(err) {
88

99
// PostgreSQL uses 5-character SQLSTATE error codes
1010
switch (err.code) {
11-
// Unique Violation (e.g., duplicate key value violates unique constraint)
12-
case '23505':
11+
// 1. Data Integrity & Constraints
12+
case '23505': // unique_violation
1313
err.code = 'UNIQUE_CONSTRAINT_VIOLATION';
1414
break;
15-
16-
// Undefined Table (relation "..." does not exist)
17-
case '42P01':
18-
err.code = 'TABLE_NOT_FOUND';
19-
break;
20-
21-
// Foreign Key Violation (violates foreign key constraint)
22-
case '23503':
23-
// Exclusion Violation
24-
case '23P01':
15+
case '23503': // foreign_key_violation
16+
case '23P01': // exclusion_violation
2517
err.code = 'FOREIGN_KEY_VIOLATION';
2618
break;
27-
28-
// Not Null Violation (null value in column violates not-null constraint)
29-
case '23502':
19+
case '23502': // not_null_violation
3020
err.code = 'NOT_NULL_VIOLATION';
3121
break;
32-
33-
// Check Violation (new row for relation violates check constraint)
34-
case '23514':
22+
case '23514': // check_violation
3523
err.code = 'CHECK_CONSTRAINT_VIOLATION';
3624
break;
3725

38-
// Data Exception (numeric value out of range, string data right truncation, invalid text representation)
39-
case '22001':
40-
case '22003':
41-
case '22P02':
26+
// 2. Schema & Object Definitions
27+
case '42P01': // undefined_table
28+
err.code = 'TABLE_NOT_FOUND';
29+
break;
30+
case '42703': // undefined_column
31+
err.code = 'COLUMN_NOT_FOUND';
32+
break;
33+
34+
// 3. Data Formatting & Types
35+
case '22001': // string_data_right_truncation
36+
case '22003': // numeric_value_out_of_range
37+
case '22P02': // invalid_text_representation
4238
err.code = 'DATA_TYPE_MISMATCH';
4339
break;
4440

45-
// Deadlock Detected / Lock Not Available
46-
case '40P01':
47-
case '55P03':
41+
// 4. Concurrency & Execution
42+
case '40001': // serialization_failure
43+
case '40P01': // deadlock_detected
44+
case '55P03': // lock_not_available
4845
err.code = 'LOCK_CONFLICT';
4946
break;
47+
case '57014': // query_canceled
48+
err.code = 'QUERY_TIMEOUT';
49+
break;
50+
case '42601': // syntax_error
51+
err.code = 'SYNTAX_ERROR';
52+
break;
5053

51-
// Connection Exception / Cannot Connect Now / Server Shut Down / Crash Shutdown
52-
case '08000':
53-
case '08003':
54-
case '08006':
55-
case '57P01':
56-
case '57P02':
57-
case '57P03':
54+
// 5. Infrastructure & Connection
55+
case '08000': // connection_exception
56+
case '08003': // connection_does_not_exist
57+
case '08006': // connection_failure
58+
case '57P01': // admin_shutdown
59+
case '57P02': // crash_shutdown
60+
case '57P03': // cannot_connect_now
5861
err.code = 'CONNECTION_FAILURE';
5962
break;
6063
}

‎test/error-normalization.test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ describe('postgresql error normalization unit tests', function() {
2222
normalized.should.have.property('code', 'TABLE_NOT_FOUND');
2323
});
2424

25+
it('normalizes undefined column errors (42703)', function() {
26+
const rawErr = new Error('column "non_existent" does not exist');
27+
rawErr.code = '42703';
28+
29+
const normalized = normalizePostgreSQLError(rawErr);
30+
normalized.should.have.property('code', 'COLUMN_NOT_FOUND');
31+
});
32+
33+
it('normalizes syntax errors (42601)', function() {
34+
const rawErr = new Error('syntax error at or near "SELCT"');
35+
rawErr.code = '42601';
36+
37+
const normalized = normalizePostgreSQLError(rawErr);
38+
normalized.should.have.property('code', 'SYNTAX_ERROR');
39+
});
40+
41+
it('normalizes query timeout errors (57014)', function() {
42+
const rawErr = new Error('canceling statement due to statement timeout');
43+
rawErr.code = '57014';
44+
45+
const normalized = normalizePostgreSQLError(rawErr);
46+
normalized.should.have.property('code', 'QUERY_TIMEOUT');
47+
});
48+
49+
it('normalizes lock conflict and deadlock errors (40P01, 40001, 55P03)', function() {
50+
['40P01', '40001', '55P03'].forEach(code => {
51+
const rawErr = new Error('lock conflict / deadlock');
52+
rawErr.code = code;
53+
const normalized = normalizePostgreSQLError(rawErr);
54+
normalized.should.have.property('code', 'LOCK_CONFLICT');
55+
});
56+
});
57+
2558
it('normalizes not-null violations (23502)', function() {
2659
const rawErr = new Error('null value in column violates not-null constraint');
2760
rawErr.code = '23502';
@@ -70,6 +103,32 @@ describe('postgresql error normalization', function() {
70103
);
71104
});
72105

106+
it('normalizes column not found errors (42703 -> COLUMN_NOT_FOUND)', function(done) {
107+
db.connector.executeSQL(
108+
'SELECT "non_existent_column" FROM "errormodel"',
109+
[],
110+
{},
111+
function(err) {
112+
should.exist(err);
113+
err.should.have.property('code', 'COLUMN_NOT_FOUND');
114+
done();
115+
},
116+
);
117+
});
118+
119+
it('normalizes syntax errors (42601 -> SYNTAX_ERROR)', function(done) {
120+
db.connector.executeSQL(
121+
'SELCT * FROM "errormodel"',
122+
[],
123+
{},
124+
function(err) {
125+
should.exist(err);
126+
err.should.have.property('code', 'SYNTAX_ERROR');
127+
done();
128+
},
129+
);
130+
});
131+
73132
it('normalizes not-null constraint violations (23502 -> NOT_NULL_VIOLATION)', function(done) {
74133
// Bypass model validation by executing raw SQL or using connector directly
75134
db.connector.executeSQL(

0 commit comments

Comments
 (0)