Registry / database / db-errors

db-errors

JSON →
library0.2.3jsnpmunverified

Unified error API for node.js SQL database drivers (MySQL, PostgreSQL, SQLite3, MSSQL). Current stable version 0.2.3. This library normalizes driver-specific errors (like unique constraint violations, not-null violations) into standard error classes, providing consistent properties such as table name, column name, and constraint name across databases. It is in early stages (pre-1.0) with limited active development, but is widely used as a dependency in ORMs like objection.js. Alternative error handling is driver-specific; this library simplifies error handling.

npm install db-errors
INSTALL
IMPORT
SIG · DB-ERRORS
D
db-errors
databasejavascriptv0.2.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

wrapError
import { wrapError } from 'db-errors'
var wrapError = require('db-errors').wrapError
Package uses CommonJS, but importing named export via ESM works with bundlers or Node >=14.
DBError
import { DBError } from 'db-errors'
Base class for all errors. Typically used in instanceof checks.
UniqueViolationError
import { UniqueViolationError } from 'db-errors'
Handles unique constraint violations. Properties: columns, table, constraint.

Wraps a native database driver error into a unified error class and handles specific violations.

const { wrapError, UniqueViolationError, NotNullViolationError } = require('db-errors'); // Assuming you have a database call that might throw function handleDatabaseError(err) { const dbError = wrapError(err); if (dbError instanceof UniqueViolationError) { console.log(`Unique constraint ${dbError.constraint} failed on table ${dbError.table} for columns ${dbError.columns}`); } else if (dbError instanceof NotNullViolationError) { console.log(`Not null constraint failed on table ${dbError.table} for column ${dbError.column}`); } else if (dbError instanceof DBError) { console.log(`Database error: ${dbError.nativeError.message}`); } else { throw err; } }
Debug
Known issues
gotchawrapError only works with errors thrown by supported drivers: MySQL, PostgreSQL, SQLite3, MSSQL. For other drivers, it returns the original error unchanged.
fix
Ensure your database driver is one of the supported ones, or check if the returned error is an instance of DBError before using driver-specific properties.
affects: >=0.0.0
gotchaProperties like columns, table, constraint are only available for certain drivers (e.g., columns for postgres/sqlite, constraint for postgres/mysql). Accessing undefined properties can lead to bugs.
fix
Always check if the property exists (e.g., if (err.columns) ...) or refer to the documentation for driver-specific availability.
affects: >=0.0.0
gotchaDataError is not thrown by SQLite due to dynamic typing. Relying on DataError for SQLite will never catch those errors.
fix
Handle SQLite errors separately or use the base DBError for fallback.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: db_error_1.wrapError is not a function
Importing as default import instead of named import.
fix
Use destructured import: import { wrapError } from 'db-errors'
Property 'columns' does not exist on type 'UniqueViolationError'
TypeScript definitions are missing or incomplete for some properties.
fix
Use type assertion: (err as any).columns or install type definitions from DefinitelyTyped if available.
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources
db-errors — npm install db-errors · libregistry