https://github.com/nihalgonsalves/pg-error-enum
TypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.
https://github.com/nihalgonsalves/pg-error-enum
javascript orm postgres postgresql psql typeorm typescript
Last synced: about 1 year ago
JSON representation
TypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.
- Host: GitHub
- URL: https://github.com/nihalgonsalves/pg-error-enum
- Owner: nihalgonsalves
- License: mit
- Created: 2019-03-15T14:57:10.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-10-02T08:51:13.000Z (over 1 year ago)
- Last Synced: 2024-10-30T06:58:14.862Z (over 1 year ago)
- Topics: javascript, orm, postgres, postgresql, psql, typeorm, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/pg-error-enum
- Size: 913 KB
- Stars: 37
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# pg-error-enum


TypeScript Enum for Postgres Errors with no runtime dependencies. Also compatible with plain JavaScript.
## Quick Start
### Installation
```sh
# Using npm
npm install --save pg-error-enum
# Using yarn
yarn add pg-error-enum
```
### Usage
```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](https://github.com/postgres/postgres/blob/master/src/backend/utils/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])\s*ERRCODE_(?[A-Z_]*)\s*(?[a-z_]*)$/;
```