Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
use logger to catch fatal errors rather then console
Browse files Browse the repository at this point in the history
  • Loading branch information
butlerx committed Oct 12, 2020
1 parent 8b1475d commit f1119c7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function (options) {
}
}
},

log: options && options.log ? options.log : undefined,
'recaptcha_secret_key': process.env.RECAPTCHA_SECRET_KEY || '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
transport: {
type: 'web',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"covrep": "bash test/covrep.sh",
"lint": "./node_modules/.bin/semistandard *.js config/config.js",
"start": "node service.js",
"dev": "nodemon service.js"
"dev": "nodemon service.js | bunyan"
},
"pre-commit": [
"test"
Expand Down
83 changes: 41 additions & 42 deletions service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
process.setMaxListeners(0);
require('events').EventEmitter.prototype._maxListeners = 100;

const newrelic = process.env.NEW_RELIC_ENABLED === 'true' ? require('newrelic') : undefined;
const senecaNR = require('seneca-newrelic');
var config = require('./config/config.js')();
if (process.env.NEW_RELIC_ENABLED === 'true') require('newrelic');

const service = 'cp-users-service';
const { logger, log } = require('cp-logs-lib')({
name: service,
level: process.env.NODE_ENV === 'production' ? 'warn' : 'info',
});
const config = require('./config/config.js')({ log });
var seneca = require('seneca')(config);
var _ = require('lodash');
var store = require('seneca-postgresql-store');
var storeQuery = require('seneca-store-query');
var service = 'cp-users-service';
var log = require('cp-logs-lib')({name: service, level: 'warn'});
config.log = log.log;
var util = require('util');
var dgram = require('dgram');

if (process.env.NODE_ENV !== 'production') {
seneca.log.info('using config', JSON.stringify(config, null, 4));
}
logger.info(config, 'config');

seneca.options(config);
seneca.decorate('customValidatorLogFormatter', require('./lib/custom-validator-log-formatter'));
Expand All @@ -29,60 +29,55 @@ if (process.env.MAILDEV_ENABLED === 'true') {
seneca.use('mail', config.email);
}

function shutdown (err) {
function shutdown(err) {
if (err !== undefined) {
var error = {
date: new Date().toString(),
msg: err.stack !== undefined
logger.error(
err,
err.stack !== undefined
? 'FATAL: UncaughtException, please report: ' + util.inspect(err.stack)
: 'FATAL: UncaughtException, no stack trace',
err: util.inspect(err)
};
console.error(JSON.stringify(error));
);
process.exit(1);
}
process.exit(0);
}

require('./migrate-psql-db.js')(function (err) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log('Migrations ok');
if (err) shutdown(err);
logger.info('Migrations ok');

seneca.use(require('./email-notifications.js'));
seneca.use(require('./lib/agreements'));
seneca.use(require('./profiles.js'),
{ postgresql: config['postgresql-store'],
logger: log.logger
});
seneca.use(require('./oauth2.js'), {clients: config.oauth2.clients});
seneca.use(require('./profiles.js'), {
postgresql: config['postgresql-store'],
logger,
});
seneca.use(require('./oauth2.js'), { clients: config.oauth2.clients });
seneca.use('user');
seneca.use('auth');
seneca.use(require('./users.js'),
{ 'email-notifications': config['email-notifications'],
'postgresql': config['postgresql-store'],
'users': config['users'],
'logger': log.logger
});
seneca.use(require('./user-profile.js'),
{ postgresql: config['postgresql-store'],
logger: log.logger
});
seneca.use(require('./users.js'), {
'email-notifications': config['email-notifications'],
postgresql: config['postgresql-store'],
users: config['users'],
logger,
});
seneca.use(require('./user-profile.js'), {
postgresql: config['postgresql-store'],
logger,
});
seneca.use(require('./nodebb-api.js'), config.nodebb);
seneca.use(require('cp-permissions-plugin'), {
config: __dirname + '/config/permissions'
config: __dirname + '/config/permissions',
});
if (!_.isUndefined(newrelic)) {
seneca.use(senecaNR, {
newrelic,
roles: ['cd-users', 'cd-profiles', 'cd-oauth2', 'cd-user-profile'],
filter (p) {
filter(p) {
p.user = p.user ? p.user.id : undefined;
p.login = p.login ? p.login.id : undefined;
return p;
}
},
});
}
process.on('SIGINT', shutdown);
Expand All @@ -103,10 +98,14 @@ require('./migrate-psql-db.js')(function (err) {

var escape = require('seneca-standard-query/lib/relational-util').escapeStr;
['load', 'list'].forEach(function (cmd) {
seneca.wrap('role: entity, cmd: ' + cmd, function filterFields (args, cb) {
seneca.wrap('role: entity, cmd: ' + cmd, function filterFields(args, cb) {
try {
['limit$', 'skip$'].forEach(function (field) {
if (args.q[field] && args.q[field] !== 'NULL' && !/^[0-9]+$/g.test(args.q[field] + '')) {
if (
args.q[field] &&
args.q[field] !== 'NULL' &&
!/^[0-9]+$/g.test(args.q[field] + '')
) {
throw new Error('Expect limit$, skip$ to be a number');
}
});
Expand All @@ -124,7 +123,7 @@ require('./migrate-psql-db.js')(function (err) {
}
if (args.q.fields$) {
args.q.fields$.forEach(function (field, index) {
args.q.fields$[index] = '\"' + escape(field) + '\"';
args.q.fields$[index] = '"' + escape(field) + '"';
});
}
// Loop over each props
Expand Down

0 comments on commit f1119c7

Please sign in to comment.