# pg-error-snippet

> Show a code snippet for `node-postgres` errors.

Latest version **0.0.2** (published 2020-01-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install pg-error-snippet
pnpm add pg-error-snippet
yarn add pg-error-snippet
bun add pg-error-snippet
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2020-01-27 |
| First published | 2020-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 149.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Dave Star |
| Maintainers | daviestar |

## Links

- npm: https://www.npmjs.com/package/pg-error-snippet
- npm.io page: https://npm.io/package/pg-error-snippet

## Dependencies (2)

- [chalk](https://npm.io/package/chalk.md) ^3.0.0
- [highlight.js](https://npm.io/package/highlight.js.md) ^9.16.2

## Recent versions

- 0.0.2 (latest) — 2020-01-27
- 0.0.1 — 2020-01-25

## README

# pg-error-snippet

Show a code snippet for `node-postgres` errors.

<img src="images/arrow-style.png">
<img src="images/highlight-style.png">

### Installation

```
npm add --save pg-error-snippet
yarn add pg-error-snippet
```

### Usage

```js
const {Pool} = require('pg')
const {handleError} = require('pg-error-snippet')
const pool = new Pool(connectionOptions)

const query = 'CREATE TABLE foo()'

pool.query(query).catch((err) => {
  handleError(err, query, options)
})
```

### Options

| Option | Default | Description |
| :--- | :--- | :--- |
| eol | '\n' | You can use '\r\n' for CRLF line endings |
| linesBefore | 4 | The max number of lines to show before the error line
| linesAfter | 0 | The max number of lines to show after the error line
| label | 'postgres' | The label displayed before the error message
| errorStyle | 'arrow' | Options are 'arrow' or 'highlight' (see image above)
| showColors | true | Enable or disable colors
| errorColor | 'red' | Choose from one of the [colours](https://github.com/chalk/chalk#colors) available in chalk
| showLineNumbers | true | Enable or disable line numbers
| lineNumberColor | 'grey' | Choose from one of the [colours](https://github.com/chalk/chalk#colors) available in chalk
| showStack | true | Enable or disable logging the error stack

### API

#### handleError

```js
const {Pool} = require('pg')
const {handleError} = require('pg-error-snippet')
const pool = new Pool(connectionOptions)

performQuery('CREATE TABLE foo)')

// using try/catch
function performQuery(query) {
  try {
    return pool.query(query)
  } catch(error) {
    handleError(error, query, options)
  }
}

// using promises
function performQuery(query) {
  return pool.query(query).catch((error) => {
    handleError(error, query, options)
  })
}

```

#### createCodeSnippet

```js
const {Pool} = require('pg')
const {createCodeSnippetFromError} = require('pg-error-snippet')
const pool = new Pool(connectionOptions)

performQuery('CREATE TABL foo()')

function performQuery(query) {
  try {
    return pool.query(query)
  } catch(error) {
    if (error.position !== undefined) {
      const snippet = createCodeSnippetFromError(error, query, options)
      console.log(
        error.message + '\n'
        snippet + '\n',
        error.stack
      )
    }
  })
}
```

---
_Source: https://npm.io/package/pg-error-snippet · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
