TypeScript Enum for Postgres Errors with no runtime dependencies
npm install pg-error-enumTypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.
``shUsing npm
npm install --save pg-error-enum
$3
`ts
import { PostgresError } from "pg-error-enum";
`
Legacy CommonJS
`js
const { PostgresError } = require("pg-error-enum");
`Usage
`ts
if (error.code === PostgresError.UNIQUE_VIOLATION) {
throw new Error("That username is taken");
}
`Generation
The Enum is generated directly from errcodes.txt in the Postgres repository.
It follows the syntax defined in the text file, i.e., in short:
1. Lines beginning with
# and empty lines are ignored.2. Sections are parsed using:
`ts
const sectionRegex = /^Section:\s(?.*)$/;
`3. Each error code is parsed using:
`ts
const errorLineRegex =
/^(?[A-Z0-9])\s(?[EWS])\sERRCODE_(?[A-Z_] )\s(?[a-z_])$/;
``