TypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.
# Using npm
npm install --save pg-error-enum
# Using yarn
yarn add pg-error-enum
TypeScript or ES6 Modules
import { PostgresError } from "pg-error-enum";
JavaScript
const PostgresError = require("pg-error-enum").PostgresError;
Usage
if (error.code === PostgresError.UNIQUE_VIOLATION) {
throw new Error("That username is taken");
}
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:
-
Lines beginning with
#
and empty lines are ignored. -
Sections are parsed using:
const sectionRegex = /^Section:\s(?<description>.*)$/;
-
Each error code is parsed using:
const errorLineRegex = /^(?<sqlstate>[A-Z0-9]*)\s*(?<severity>[EWS])\s*ERRCODE_(?<constant>[A-Z_]*)\s*(?<code>[a-z_]*)$/;