Code Monkey home page Code Monkey logo

drboom-pg's Introduction

Dr. Boom: Postgres

npm i pgboom

Auto-cast errors from pg into Hapi HTTP Errors.

##Example / Boilerplate

Plugin registration boilerplate:

var hapi = require('hapi');
var config = require('./config.json');
var Boom = require('boom');

var server = new hapi.Server();
server.connection(config);

server.register([{
    register: require('drboom')(), 
    options: {
        plugins: [require('drboom-pg')({getNull404: true,
            extendConditions: {
                '2201W': function (err) {
                    return Boom.badRequest('Invalid pagination values');
                }
            },
            extendCategories: {
                '28': function (err) {
                    return Boom.unauthorized('Invalid credentials');
                }
            }
        })]
    },
}, function (err) {
    server.log(['startup'], 'Loaded pgboom plugin');
    server.start(function (err) {
        //...
    }
});

Now, you can pass your postgres errors right on through to hapi reply!

function someHandler(request, reply) {
    pg.query("SELECT id FROM request.params.id", function (err, results) {
        reply(err, results.rows);
    });
});

Options

getNull404: Boolean. If the handler request method is GET, and postgres doesn't pass a result, throw a 404 Not Found error regardless of there not being a postgres error. Default: False

extendConditions: Object. Keys of postgres error codes, mapped to functions that return Boom objects.

extendCategories: Object. Keys of the first 2 digits of error codes, mapped to functions that return Boom objects.

Default Conditions and Categories

var condition = {
    '23505': function (err) {
        return Boom.conflict(util.format("Failed relationship constraint: %s", err.constraint));
    }, 
    '42501': function (err) {
        return Boom.forbidden(err.toString());
    }
}

var category = {
    '08': function (err) {
        return Boom.serverTimeout('Database unavailable');
    },
    '53': function (err) {
        return Boom.serverTimeout('Database unavailable');
    },
    '22': function (err) {
        return Boom.badData(err.toString());
    },
    '23': function (err) {
        return Boom.badData(err.constraint);
    }
}

I'll gladly accept pull requests!

drboom-pg's People

Contributors

fritzy avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

nlf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.