Skip to content

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

License

Notifications You must be signed in to change notification settings

nihalgonsalves/pg-error-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0e75ce7 · Sep 11, 2023
Aug 3, 2023
Jul 1, 2023
Sep 11, 2023
Mar 15, 2019
Jul 1, 2023
Apr 25, 2021
Jul 7, 2023
Apr 25, 2021
Sep 11, 2023
Mar 15, 2019
Jul 1, 2023
Aug 3, 2023
Sep 11, 2023
Sep 11, 2023
Mar 29, 2021
Apr 25, 2021

Repository files navigation

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

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');
}

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_]*)$/;