Skip to content

Commit

Permalink
Merge pull request #4 from Vincit/greenkeeper/initial
Browse files Browse the repository at this point in the history
Update dependencies to enable Greenkeeper 馃尨
  • Loading branch information
elhigu committed Apr 20, 2017
2 parents 4eeb805 + d94b24e commit b0435d7
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 23 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_js:
- '4'
- '5'
- '6'
- '7'

after_script:
- npm run coveralls
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

# Dodo.js core features

[![Greenkeeper badge](https://badges.greenkeeper.io/Vincit/dodo-core-features.svg)](https://greenkeeper.io/)

Default features for [Dodo.js](https://github.com/vincit/dodo.js).
147 changes: 147 additions & 0 deletions error-handler/error-handler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
var _ = require('lodash')
, expect = require('expect.js')
, Promise = require('bluebird')
, sinon = require('sinon')
, middleware = require('./index')
, logger = require('dodo/logger')
, HttpError = require('dodo/errors').HTTPError
;

var mockApp = {
use: function (cb) {
mockApp.expressHandler = cb;
},
};

var mockReq = {
app: {
config: {
profile: 'production'
}
}
}

var mockRes = {
status: function (code) {
mockRes.lastStatus = code;
return {
json: function (err) {
mockRes.lastErr = err;
}
}
}
}

var config = {
headerPaths: [
'./handlers'
],
handlers: [
'http',
'postgres',
'sqlite'
]
};

describe('error-handlers express middleware', function () {

var mockLogger = new logger.MockHandler();

before(function () {
// register middleware to mockapp
middleware(mockApp, config);

logger.LogHub.clearHandlers();
logger.LogHub.setHandler([
logger.level.trace,
logger.level.debug,
logger.level.info,
logger.level.warning,
logger.level.error,
], '', mockLogger);
});

beforeEach(function () {
mockLogger.flush();
});

it('should call next if no error', function () {
var next = sinon.spy();
mockApp.expressHandler(undefined, mockReq, mockRes, next);
expect(next.called).to.be.ok();
});

it('should pass HTTP error 500 if no special handler found', function () {
var next = sinon.spy();
var theError = new Error();
mockApp.expressHandler(theError, mockReq, mockRes, next);
expect(next.called).to.not.be.ok();
expect(mockLogger.logs).to.have.length(1);
expect(mockLogger.logs[0].metadata.error).to.be(theError);
});

it('should create http error response if error has statusCode, httpStatusCode or HTTPStatusCode attribute', function () {
var theError = new Error();
theError.statusCode = 404;
theError.data = 'Got the data';
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.name).to.be('Not Found');
expect(mockRes.lastErr.data).to.be(theError.data);

theError = new Error();
theError.httpStatusCode = 404;
theError.data = 'Got the data';
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.name).to.be('Not Found');
expect(mockRes.lastErr.data).to.be(theError.data);

var theError = new Error();
theError.HTTPStatusCode = 404;
theError.data = 'Got the data';
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.name).to.be('Not Found');
expect(mockRes.lastErr.data).to.be(theError.data);

var theError = new Error();
theError.status = 404;
theError.data = 'Got the data';
mockLogger.flush();
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.name).to.be('Internal Server Error');
expect(mockRes.lastErr.data).to.be(null);
expect(mockLogger.logs).to.have.length(1);
});

it('should parse sqlite unique violation error', function () {
var theError = new Error('SQLITE_CONSTRAINT: UNIQUE diipadaa dappa huh.hah.hei');
theError.errno = 19;
theError.code = 'SQLITE_WAT';
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.data.hah).to.be.ok();
expect(mockRes.lastErr.statusCode).to.be(409);
});

it('should match parseForeignKeyViolationError postgres error', function () {
var theError = new Error('SQLITE_CONSTRAINT: UNIQUE diipadaa dappa huh.hah.hei');
theError.severity = 'pretty bad';
theError.code = '23503';
theError.detail = '(nasty)=(somthing) "poopy" did hit the fan';
theError.internalQuery = 'select * from fan where not poo';
theError.routine = 1;
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.data.nasty).to.be.ok();
expect(mockRes.lastErr.statusCode).to.be(409);
});

it('should match parseUniqueViolationError postgres error', function () {
var theError = new Error('SQLITE_CONSTRAINT: UNIQUE diipadaa dappa huh.hah.hei');
theError.severity = 'pretty bad';
theError.code = '23505';
theError.detail = '(nasty)=(somthing) "poopy" did hit the fan';
theError.internalQuery = 'select * from fan where not poo';
theError.routine = 1;
mockApp.expressHandler(theError, mockReq, mockRes, null);
expect(mockRes.lastErr.data.nasty).to.contain('already exists');
expect(mockRes.lastErr.statusCode).to.be(409);
});
});
2 changes: 1 addition & 1 deletion error-handler/handlers/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function(error) {
function isPostgresError(error) {
if (!error) { return false; }
// Just check the existence of a bunch of attributes. There doesn't seem to be an easier way.
return _.all(['severity', 'code', 'detail', 'internalQuery', 'routine'], function(attr) {
return _.every(['severity', 'code', 'detail', 'internalQuery', 'routine'], function(attr) {
return _.has(error, attr);
});
}
Expand Down
5 changes: 3 additions & 2 deletions error-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var _ = require('lodash')
, fs = require('fs')
, path = require('path')
, HTTPError = require('dodo/errors').HTTPError;
, HTTPError = require('dodo/errors').HTTPError
, log = require('dodo/logger').getLogger('dodo-core-features.error-handler');

/**
* Registers an *Express* middleware that catches errors and creates error responses.
Expand Down Expand Up @@ -67,7 +68,7 @@ module.exports = function(app, config) {
// No handler found. Send generic 500.
var debugging = req.app.config.profile !== 'production';
errorResponse = new HTTPError(500, debugging ? err.stack : null).toJSON();
console.error(err.stack);
log.trace({ error: err }, "Unknown error");
}

res.status(errorResponse.statusCode || 500).json(errorResponse);
Expand Down
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,41 @@
"plugin"
],
"dependencies": {
"aws-sdk": "2.1.26",
"bluebird": "2.9.25",
"body-parser": "1.12.3",
"cli-color": "1.0.0",
"compression": "1.4.3",
"cookie-parser": "1.3.4",
"aws-sdk": "2.12.0",
"bluebird": "3.4.7",
"body-parser": "1.16.1",
"cli-color": "1.1.0",
"compression": "1.6.2",
"cookie-parser": "1.4.3",
"expect.js": "0.3.1",
"express-session": "1.11.1",
"fs-extra": "0.18.2",
"glob": "5.0.5",
"lodash": "^3.6.0",
"morgan": "1.5.2",
"mv": "2.0.3",
"node-uuid": "1.4.3",
"passport": "0.2.1",
"passport-http": "0.2.2",
"express-session": "1.15.1",
"fs-extra": "2.0.0",
"glob": "7.1.1",
"lodash": "^4.17.4",
"morgan": "1.8.1",
"mv": "2.1.1",
"uuid": "3.0.1",
"passport": "0.3.2",
"passport-http": "0.3.0",
"passport-local": "1.0.0",
"redis": "0.12.1",
"redis": "2.6.5",
"underscore.string": "^3.0.0"
},
"devDependencies": {
"dodo": "^0.0.11",
"chai": "^3.4.0",
"coveralls": "^2.11.4",
"dodo": "^0.0.12",
"express": "^4.12.0",
"istanbul": "^0.4.0",
"mocha": "2.2.4"
"mocha": "3.2.0",
"sinon": "^2.1.0"
},
"peerDependencies": {
"express": "^4.12.0"
},
"scripts": {
"test": "node_modules/.bin/istanbul --config=.istanbul.yml cover _mocha -- --slow 10 --timeout 5000 --reporter spec --recursive ./**/tests/*.js",
"test-no-coverage": "mocha --slow 10 --timeout 5000 --reporter spec --recursive --bail ./**/tests/*.js",
"test": "node_modules/.bin/istanbul --config=.istanbul.yml cover _mocha -- --slow 10 --timeout 5000 --reporter spec --recursive ./**/*.spec.js",
"test-no-coverage": "mocha --slow 10 --timeout 5000 --reporter spec --recursive --bail ./**/*.spec.js",
"coveralls": "cat ./test-coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
}
}

0 comments on commit b0435d7

Please sign in to comment.