pg-error-enum

1.0.2 • Public • Published

pg-error-enum

npm version build status

TypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.

Quick Start

Installation

# Using npm
npm install --save pg-error-enum

# Using yarn
yarn add pg-error-enum

Usage

import { PostgresError } from "pg-error-enum";
Legacy CommonJS
const { PostgresError } = require("pg-error-enum");

Usage

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:

    const sectionRegex = /^Section:\s(?<description>.*)$/;
  3. Each error code is parsed using:

    const errorLineRegex =
      /^(?<sqlstate>[A-Z0-9]*)\s*(?<severity>[EWS])\s*ERRCODE_(?<constant>[A-Z_]*)\s*(?<code>[a-z_]*)$/;

Readme

Keywords

none

Package Sidebar

Install

npm i pg-error-enum

Weekly Downloads

18,366

Version

1.0.2

License

MIT

Unpacked Size

80.8 kB

Total Files

9

Last publish

Collaborators

  • nihalgonsalves