@slonik/pg-driver
Advanced tools
Comparing version 46.1.0 to 46.2.0
@@ -12,3 +12,2 @@ "use strict"; | ||
const node_stream_1 = require("node:stream"); | ||
// eslint-disable-next-line no-restricted-imports | ||
const pg_1 = require("pg"); | ||
@@ -163,2 +162,5 @@ const pg_query_stream_1 = __importDefault(require("pg-query-stream")); | ||
} | ||
if (error.code === '23P01') { | ||
return new errors_1.CheckExclusionConstraintViolationError(error); | ||
} | ||
if (error.code === '23514') { | ||
@@ -256,3 +258,2 @@ return new errors_1.CheckIntegrityConstraintViolationError(error); | ||
} | ||
// eslint-disable-next-line @babel/no-invalid-this | ||
this.push({ | ||
@@ -259,0 +260,0 @@ fields, |
export { createPgDriverFactory } from './factories/createPgDriverFactory'; | ||
export { DatabaseError } from 'pg'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPgDriverFactory = void 0; | ||
exports.DatabaseError = exports.createPgDriverFactory = void 0; | ||
var createPgDriverFactory_1 = require("./factories/createPgDriverFactory"); | ||
Object.defineProperty(exports, "createPgDriverFactory", { enumerable: true, get: function () { return createPgDriverFactory_1.createPgDriverFactory; } }); | ||
var pg_1 = require("pg"); | ||
Object.defineProperty(exports, "DatabaseError", { enumerable: true, get: function () { return pg_1.DatabaseError; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -19,9 +19,9 @@ { | ||
"dependencies": { | ||
"@slonik/driver": "^46.1.0", | ||
"@slonik/errors": "^46.1.0", | ||
"@slonik/sql-tag": "^46.1.0", | ||
"@slonik/types": "^46.1.0", | ||
"@slonik/utilities": "^46.1.0", | ||
"pg": "^8.12.0", | ||
"pg-query-stream": "^4.6.0", | ||
"@slonik/driver": "^46.2.0", | ||
"@slonik/errors": "^46.2.0", | ||
"@slonik/sql-tag": "^46.2.0", | ||
"@slonik/types": "^46.2.0", | ||
"@slonik/utilities": "^46.2.0", | ||
"pg": "^8.13.1", | ||
"pg-query-stream": "^4.7.1", | ||
"pg-types": "^4.0.2", | ||
@@ -32,11 +32,11 @@ "postgres-array": "^3.0.2" | ||
"devDependencies": { | ||
"@types/node": "^18.15.3", | ||
"@types/pg": "^8.11.6", | ||
"@types/node": "^22.9.0", | ||
"@types/pg": "^8.11.10", | ||
"ava": "^6.1.3", | ||
"cspell": "^8.6.0", | ||
"eslint": "^8.57.0", | ||
"eslint-config-canonical": "^42.8.1", | ||
"cspell": "^8.16.0", | ||
"eslint": "^9.14.0", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.6.3", | ||
"@slonik/eslint-config": "^46.2.0" | ||
}, | ||
@@ -84,3 +84,3 @@ "engines": { | ||
"types": "./dist/index.d.ts", | ||
"version": "46.1.0", | ||
"version": "46.2.0", | ||
"scripts": { | ||
@@ -87,0 +87,0 @@ "build": "rm -fr ./dist && tsc --project ./tsconfig.json", |
@@ -6,2 +6,41 @@ # Slonik Driver for `pg` | ||
createPgDriverFactory, | ||
} from '@slonik/pg-driver'; | ||
DatabaseError, | ||
} from '@slonik/pg-driver'; | ||
``` | ||
## Error handling | ||
All Slonik errors extend from `SlonikError`. `SlonikError` uses `cause` property to store the original error, which might be a `DatabaseError` from `pg`. Example: | ||
```ts | ||
pool.on('error', (error) => { | ||
const cause = error.cause; | ||
if (cause instanceof DatabaseError) { | ||
console.log(cause.code); | ||
} | ||
}); | ||
``` | ||
This allows you to handle errors based on the lower-level error codes provided by `pg` driver. | ||
Example of handling all errors that could warrant a fatal error: | ||
```ts | ||
const fatalErrorClasses = [ | ||
// Connection Exception | ||
'08', | ||
// Invalid Authorization Specification | ||
'28', | ||
]; | ||
pool.on('error', (error) => { | ||
if (error.cause instanceof DatabaseError) { | ||
const classCode = error.cause.code.slice(0, 2); | ||
if (fatalErrorClasses.includes(classCode)) { | ||
// Initiate shutdown due to unexpected connection state. | ||
} | ||
} | ||
}); | ||
``` |
@@ -13,2 +13,3 @@ /* eslint-disable canonical/id-match */ | ||
BackendTerminatedUnexpectedlyError, | ||
CheckExclusionConstraintViolationError, | ||
CheckIntegrityConstraintViolationError, | ||
@@ -29,7 +30,6 @@ ForeignKeyIntegrityConstraintViolationError, | ||
import { Transform } from 'node:stream'; | ||
// eslint-disable-next-line no-restricted-imports | ||
import { | ||
Client, | ||
type DatabaseError, | ||
type ClientConfig as NativePostgresClientConfiguration, | ||
type DatabaseError, | ||
} from 'pg'; | ||
@@ -192,3 +192,3 @@ import QueryStream from 'pg-query-stream'; | ||
// @see https://github.com/gajus/slonik/issues/557 | ||
const wrapError = (error: Error, query: Query | null) => { | ||
const wrapError = (error: Error, query: null | Query) => { | ||
if ( | ||
@@ -244,2 +244,6 @@ error.message.toLowerCase().includes('connection terminated unexpectedly') | ||
if (error.code === '23P01') { | ||
return new CheckExclusionConstraintViolationError(error); | ||
} | ||
if (error.code === '23514') { | ||
@@ -362,3 +366,2 @@ return new CheckIntegrityConstraintViolationError(error); | ||
// eslint-disable-next-line @babel/no-invalid-this | ||
this.push({ | ||
@@ -365,0 +368,0 @@ fields, |
export { createPgDriverFactory } from './factories/createPgDriverFactory'; | ||
export { DatabaseError } from 'pg'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37991
630
45
Updated@slonik/driver@^46.2.0
Updated@slonik/errors@^46.2.0
Updated@slonik/sql-tag@^46.2.0
Updated@slonik/types@^46.2.0
Updated@slonik/utilities@^46.2.0
Updatedpg@^8.13.1
Updatedpg-query-stream@^4.7.1