How to use the pg-promise.errors function in pg-promise

To help you get started, we’ve selected a few pg-promise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vitaly-t / pg-promise / test / typescript / errors.ts View on Github external
/// 

import * as pgPromise from 'pg-promise';

var result = new pgPromise.errors.QueryResultError();
var resultCheck = result instanceof pgPromise.errors.QueryResultError;

var query = new pgPromise.errors.QueryFileError();
var queryCheck = result instanceof pgPromise.errors.QueryFileError;
var line = query.error.position.line;

var ps = new pgPromise.errors.PreparedStatementError();
var queryCheck = result instanceof pgPromise.errors.PreparedStatementError;
var file = ps.error.file;

var qrec = pgPromise.errors.queryResultErrorCode;
var t = qrec.multiple;
github vitaly-t / pg-promise / test / typescript / prepared.ts View on Github external
var qf = new pgPromise.QueryFile('file');
var ps4 = new pgp.PreparedStatement({name: '', text: qf});

db.one(ps1);

db.one({
    name: '',
    text: ''
});

db.one({
    name: '',
    text: qf
});

var w = pgPromise.errors.PreparedStatementError;

// TODO: need to figure out how to export the type namespace;
//var test1 = ps1.parse();
github vitaly-t / pg-promise / test / typescript / errors.ts View on Github external
/// 

import * as pgPromise from 'pg-promise';

var result = new pgPromise.errors.QueryResultError();
var resultCheck = result instanceof pgPromise.errors.QueryResultError;

var query = new pgPromise.errors.QueryFileError();
var queryCheck = result instanceof pgPromise.errors.QueryFileError;
var line = query.error.position.line;

var ps = new pgPromise.errors.PreparedStatementError();
var queryCheck = result instanceof pgPromise.errors.PreparedStatementError;
var file = ps.error.file;

var qrec = pgPromise.errors.queryResultErrorCode;
var t = qrec.multiple;
github jamesplease / api-pls / packages / api-pls-postgres-adapter / util / map-pgp-error.js View on Github external
'use strict';

const _ = require('lodash');
const pgp = require('pg-promise');
const serverErrors = require('../../api-pls-utils/server-errors');

const queryErrorCode = pgp.errors.queryResultErrorCode;

const pgPromiseErrors = {
  noData: serverErrors.notFound,
  notEmpty: serverErrors.generic,
  multiple: serverErrors.generic
};

// Pass in a `codeToFind` (a code sent back from pg-promise),
// and you'll get back an `errorDefinition` from `serverErrors`.
module.exports = function(codeToFind) {
  var key = _.findKey(queryErrorCode, c => c === codeToFind);
  return pgPromiseErrors[key];
};
github jamesplease / api-pls / packages / api-pls-postgres-adapter / util / handle-query-error.js View on Github external
module.exports = function({err, req, crudAction, query, selfLink}) {
  const pls = req.pls;
  var serverError;

  // First, check to see if it's a pgp QueryResultError. If it
  // is, we generate the appropriate server error.
  if (err instanceof pgp.errors.QueryResultError) {
    serverError = mapPgError(err.code);
  }

  // If it's not a pgp QueryResultError, we send over tbe generic server error.
  else {
    serverError = serverErrors.generic;
  }

  const dataToSend = {
    errors: [serverError.body()]
  };

  if (selfLink) {
    dataToSend.links = {
      self: selfLink
    };
github jamesplease / api-pls / packages / api-pls / server / resource / controller.js View on Github external
function handleQueryError(err, res, resource, crudAction) {
  var serverError;

  // First, check to see if it's a pgp QueryResultError. If it
  // is, we generate the appropriate server error.
  if (err instanceof pgp.errors.QueryResultError) {
    serverError = mapPgError(err.code);
  }

  // If it's not a pgp QueryResultError, we send over tbe generic server error.
  else {
    serverError = serverErrors.generic;
  }

  log.warn({
    resourceName: resource.name,
    err, crudAction
  }, 'There was a query error with a CRUD request.');
  res.status(serverError.code);
  sendJson(res, {
    errors: [serverError.body()]
  });
github jamesplease / api-pls / test / unit / server / util / handle-query-error.js View on Github external
it('should return the mapped error', () => {
      const req = {};
      const definition = {};

      handleQueryError({
        err: new pgp.errors.QueryResultError(0, {rows: {}}),
        res: this.res,
        req, definition
      });

      assert(this.res.status.calledOnce);
      assert(this.res.status.calledWithExactly(2));
      assert(this.sendJsonStub.calledOnce);
      assert(this.sendJsonStub.calledWith(this.res, {
        errors: [{
          hungry: false
        }]
      }));
    });
  });
github Half-Shot / matrix-appservice-discord / src / db / postgres.ts View on Github external
public async All(sql: string, parameters?: ISqlCommandParameters): Promise {
        log.silly("All:", sql);
        try {
            return await this.db.many(Postgres.ParameterizeSql(sql), parameters);
        } catch (ex) {
            if (ex.code === pgPromise.errors.queryResultErrorCode.noData ) {
                return [];
            }
            throw ex;
        }
    }