|
| 1 | +'use strict'; |
| 2 | +require('./init.js'); |
| 3 | +const should = require('should'); |
| 4 | + |
| 5 | +let db, ErrorModel; |
| 6 | +const normalizePostgreSQLError = require('../lib/normalize-error.js'); |
| 7 | + |
| 8 | +describe('postgresql error normalization unit tests', function() { |
| 9 | + it('normalizes unique constraint violations (23505)', function() { |
| 10 | + const rawErr = new Error('duplicate key value violates unique constraint'); |
| 11 | + rawErr.code = '23505'; |
| 12 | + |
| 13 | + const normalized = normalizePostgreSQLError(rawErr); |
| 14 | + normalized.should.have.property('code', 'UNIQUE_CONSTRAINT_VIOLATION'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('normalizes undefined table errors (42P01)', function() { |
| 18 | + const rawErr = new Error('relation "foo" does not exist'); |
| 19 | + rawErr.code = '42P01'; |
| 20 | + |
| 21 | + const normalized = normalizePostgreSQLError(rawErr); |
| 22 | + normalized.should.have.property('code', 'TABLE_NOT_FOUND'); |
| 23 | + }); |
| 24 | + |
| 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 | + |
| 58 | + it('normalizes not-null violations (23502)', function() { |
| 59 | + const rawErr = new Error('null value in column violates not-null constraint'); |
| 60 | + rawErr.code = '23502'; |
| 61 | + |
| 62 | + const normalized = normalizePostgreSQLError(rawErr); |
| 63 | + normalized.should.have.property('code', 'NOT_NULL_VIOLATION'); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('postgresql error normalization', function() { |
| 68 | + before(function(done) { |
| 69 | + db = global.getDataSource(true); |
| 70 | + // Explicitly configure unique index so automigrate creates the constraint in Postgres |
| 71 | + ErrorModel = db.define('ErrorModel', { |
| 72 | + name: {type: String, index: {unique: true}, required: true}, |
| 73 | + }); |
| 74 | + db.automigrate('ErrorModel', done); |
| 75 | + }); |
| 76 | + |
| 77 | + beforeEach(function(done) { |
| 78 | + ErrorModel.destroyAll(done); |
| 79 | + }); |
| 80 | + |
| 81 | + it('normalizes unique constraint violations (23505 -> UNIQUE_CONSTRAINT_VIOLATION)', function(done) { |
| 82 | + ErrorModel.create({name: 'test-unique'}, function(err) { |
| 83 | + if (err) return done(err); |
| 84 | + // Attempt to insert duplicate record |
| 85 | + ErrorModel.create({name: 'test-unique'}, function(err) { |
| 86 | + should.exist(err); |
| 87 | + err.should.have.property('code', 'UNIQUE_CONSTRAINT_VIOLATION'); |
| 88 | + done(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('normalizes table not found errors (42P01 -> TABLE_NOT_FOUND)', function(done) { |
| 94 | + db.connector.executeSQL( |
| 95 | + 'SELECT * FROM "non_existent_table_xyz"', |
| 96 | + [], |
| 97 | + {}, |
| 98 | + function(err, results) { |
| 99 | + should.exist(err); |
| 100 | + err.should.have.property('code', 'TABLE_NOT_FOUND'); |
| 101 | + done(); |
| 102 | + }, |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 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 | + |
| 132 | + it('normalizes not-null constraint violations (23502 -> NOT_NULL_VIOLATION)', function(done) { |
| 133 | + // Bypass model validation by executing raw SQL or using connector directly |
| 134 | + db.connector.executeSQL( |
| 135 | + 'INSERT INTO "errormodel" ("name") VALUES ($1)', |
| 136 | + [null], |
| 137 | + {}, |
| 138 | + function(err) { |
| 139 | + should.exist(err); |
| 140 | + err.should.have.property('code', 'NOT_NULL_VIOLATION'); |
| 141 | + done(); |
| 142 | + }, |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it('normalizes inactive transaction or connection errors at the connector level', function(done) { |
| 147 | + const Transaction = require('loopback-connector').Transaction; |
| 148 | + Transaction.begin(db.connector, function(err, tx) { |
| 149 | + if (err) return done(err); |
| 150 | + |
| 151 | + // Force commit the transaction so its connection is cleared |
| 152 | + tx.commit(function(err) { |
| 153 | + if (err) return done(err); |
| 154 | + |
| 155 | + // Call executeSQL directly to exercise the connector's internal check |
| 156 | + db.connector.executeSQL('SELECT 1', [], {transaction: tx}, function(err) { |
| 157 | + should.exist(err); |
| 158 | + err.should.have.property('code', 'TRANSACTION_NOT_ACTIVE'); |
| 159 | + done(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments