Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanleecode/postgres-error-codes
Postgres Error Codes
https://github.com/ryanleecode/postgres-error-codes
Last synced: 5 days ago
JSON representation
Postgres Error Codes
- Host: GitHub
- URL: https://github.com/ryanleecode/postgres-error-codes
- Owner: ryanleecode
- License: mit
- Created: 2019-12-29T18:24:46.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-30T13:07:35.000Z (almost 4 years ago)
- Last Synced: 2024-10-13T01:19:59.571Z (about 1 month ago)
- Language: TypeScript
- Size: 28.3 KB
- Stars: 25
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Postgres Error Codes
![actions-badge](https://github.com/drdgvhbh/postgres-error-codes/workflows/CI/badge.svg)
Postgres error codes mapping for NodeJS.
## Install
```
npm i @drdgvhbh/postgres-error-codes
```## Usage
Each condition from the [PG documentation](https://www.postgresql.org/docs/9.2/errcodes-appendix.html) is available in the module. In order to obtain the status code use the prefixed uppercase condition name:
`unique_violation` => `PG_UNIQUE_VIOLATION`
`not_null_violation` => `PG_NOT_NULL_VIOLATION`
```javascript
const { PG_UNIQUE_VIOLATION, PG_NOT_NULL_VIOLATION } = require('postgres-error-codes')async function createUserMethod(req, res, next) {
try {
// Run insert user SQL
} catch (err) {
// If user with same email already exists
if (err.code === PG_UNIQUE_VIOLATION) {
return next('Email already exists!')
}// Param should not be null
if (err.code === PG_NOT_NULL_VIOLATION) {
return next('Email required!')
}next(err)
}
}
```## Related
- [PostgreSQL Error Codes Documentation](https://www.postgresql.org/docs/9.2/errcodes-appendix.html)