npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pg-error

v1.1.0

Published

Error class that parses PostgreSQL's ErrorResponse format and sets human readable field names. Works with node-pg, too.

Downloads

3,335

Readme

PgError.js

NPM version Build status

PgError.js is an error class for Node.js that parses PostgreSQL's ErrorResponse format and names its fields with human readable properties. It's most useful when combined with Brian Carlson's Node.js PostgreSQL client library to get structured and identifiable PostgreSQL errors. Supports all fields returned by PostgreSQL up to v9.4.

The PostgreSQL client library does return an Error object with some fields set, but it's not a dedicated object for easy instanceof identification nor does it yet support all field types. I've found it immensely useful to be strict and classify errors beforehand when building fault tolerant systems. That helps programmatically decide whether to wrap, escalate or handle a particular error.

Installing

npm install pg-error

PgError.js follows semantic versioning, so feel free to depend on its major version with something like >= 1.0.0 < 2 (a.k.a ^1.0.0).

Using

var PgError = require("pg-error")

var error = new PgError({
  M: "null value in column \"name\" violates not-null constraint",
  S: "ERROR",
  C: "23502"
})

error instanceof PgError // => true
error instanceof Error // => true

error.message // => "null value in column \"name\" violates not-null constraint"
error.severity // => "ERROR"
error.code // => 23502

Parsing PostgreSQL's ErrorResponse

var msg = new Buffer("MWash your teeth!\0SWARNING\0\0")
var error = PgError.parse(msg)
error.message // => "Wash your teeth!"
error.severity // => "WARNING"

Using with Node.js PostgreSQL client library

The client does its error and notice parsing on the Pg.Connection object. You can get an active instance of it after connecting from Pg.Client:

var Pg = require("pg")
var pg = new Pg.Client({host: "/tmp", database: "pg_error_test"})
var connection = pg.connection

You'll have to swap out two functions, Pg.Connection.prototype.parseE and Pg.Connection.prototype.parseN, for parsing errors and notices respectively:

connection.parseE = PgError.parse
connection.parseN = PgError.parse

If you want every connection instance to parse errors to PgError, set them on the Pg.Connection prototype:

Pg.Connection.prototype.parseE = PgError.parse
Pg.Connection.prototype.parseN = PgError.parse

However, the way the client is built, it will start emitting those errors and notices under the PgError event name. Until that's improved in the Pg.Connection class, you'll need to re-emit those under the correct error and notice events:

function emitPgError(err) {
  switch (err.severity) {
    case "ERROR":
    case "FATAL":
    case "PANIC": return this.emit("error", err)
    default: return this.emit("notice", err)
  }
}

connection.on("PgError", emitPgError)

That's it. Your Pg query errors should now be instances of PgError and with all the human readable field names.

Using with Knex.js

Using PgError.js with Knex.js is similar to using it with the plain Node.js PostgreSQL client library described above. Because Knex.js has a connection pool, you'll have to hook PgError.js in on every newly created connection:

  var Knex = require("knex")

  Knex({
    pool: {
      min: 1,
      max: 10,
      afterCreate: function(connection, done) {
        connection.connection.parseE = PgError.parse
        connection.connection.parseN = PgError.parse
        connection.connection.on("PgError", emitPgError)
        done()
      }
    }
  })

The emitPgError function is listed above.

Properties on an instance of PgError

For descriptions of the properties, please see PostgreSQL's Error and Notice Message Fields.

Property | Field | Description -----------------|---|---------------- severity | S | code | C | condition | | Code name in lowercase according to errcodes.txt. detail | D | hint | H | position | P | Position parsed to a Number. internalPosition | p | Internal position parsed to a Number. internalQuery | q | where | W | schema | s | table | t | column | c | dataType | d | constraint | n | file | F | line | L | Line parsed to a Number. routine | R |

License

PgError.js is released under a Lesser GNU Affero General Public License, which in summary means:

  • You can use this program for no cost.
  • You can use this program for both personal and commercial reasons.
  • You do not have to share your own program's code which uses this program.
  • You have to share modifications (e.g. bug-fixes) you've made to this program.

For more convoluted language, see the LICENSE file.

About

Andri Möll typed this and the code.
Monday Calendar supported the engineering work.

If you find PgError.js needs improving, please don't hesitate to type to me now at [email protected] or create an issue online.