Skip to content

daviestar/pg-error-snippet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg-error-snippet

Show a code snippet for node-postgres errors.

Installation

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

Usage

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 available in chalk
showLineNumbers true Enable or disable line numbers
lineNumberColor 'grey' Choose from one of the colours available in chalk
showStack true Enable or disable logging the error stack

API

handleError

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

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published