Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/helpers/hydrater.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var rarity = require('rarity');
var util = require('util');

var lib = require('../index.js');

var HydrationError = lib.HydrationError;
var logError = require('../utils').logError;



Expand Down Expand Up @@ -52,6 +52,12 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
if(!cleaner.called) {
cleaner.called = true;
if(err) {
var extra = JSON.parse(JSON.stringify(task));
extra.changes = changes;
extra.stdout = stdout;
extra.stderr = stderr;

logError(err, extra);
child.reset();
}
else {
Expand Down Expand Up @@ -170,7 +176,7 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
cb(null, changes);
},
function patchDocument(changes, cb) {
// Returning null means we won't complete the hdyration, and are waiting for something else.
// Returning null means we won't complete the hydration, and are waiting for something else.
if(changes === null) {
logger("Skipped task: " + ((task.file_path) ? task.file_path : task.document.id));
return cb();
Expand All @@ -187,8 +193,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
}
], function handleErrors(err, changes, res) {
async.waterfall([
function logError(cb) {
function logErrors(cb) {
if(err) {
var extra = JSON.parse(JSON.stringify(task));
extra.changes = changes;
logError(err, extra);
errLogger("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id), err.toString());
}

Expand All @@ -214,7 +223,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
}
}
], function(internalErr) {
/* istanbul ignore next */
if(internalErr) {
var extra = JSON.parse(JSON.stringify(task));
extra.changes = changes;
logError(internalErr, extra);
errLogger("INTERNAL ERR", internalErr);
}
done(err || internalErr, changes);
Expand Down
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var restify = require('restify');
var yaqs = require('yaqs');
var Childs = require('./helpers/Childs');

var utils = require('./utils.js');
/**
* Create a new hydration server.
* This server will use `config.hydrater_function` as its main function, to turn a file into metadata.
Expand All @@ -23,6 +24,14 @@ module.exports.createServer = function(config) {
config.logger = config.logger || console.log;
config.errLogger = config.errLogger || console.error;

utils.logError.config = config;

/* istanbul ignore next */
if(config.opbeat && config.opbeat.secretToken) {
var opbeat = require('opbeat');
utils.logError.opbeat = opbeat(config.opbeat);
}

var concurrency = config.concurrency || 1;
var tasksPerProcess = config.tasksPerProcess || 100;

Expand Down
70 changes: 70 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";

/* istanbul ignore next */
module.exports.logError = function logError(err, req, extra) {
// No logging on test or if err is undefined
if(process.env.NODE_ENV === "test" || !err) {
return;
}

if(!extra) {
extra = req;
req = null;
}

delete err.domain;
delete err.domainThrown;

if(err.__alreadyLogged) {
console.warn("Skipping an error already sent to Opbeat: ", err.toString());
return;
}

if(!extra) {
extra = {};
}

if(module.exports.logError.config) {
extra.hydrater = module.exports.logError.config.hydraterUrl;
}

if(module.exports.logError.opbeat) {
var meta = {
extra: extra
};

if(req) {
meta.request = req;

if(req.token) {
meta.user = {
is_authenticated: true,
id: req.token.anyfetchToken,
username: req.token.accountName,
email: req.token.accountName
};
}
}

module.exports.logError.opbeat.captureError(err, meta);
}
else {
var all = {
details: err.toString(),
err: err,
extra: extra
};

try {
all = JSON.stringify(all);
}
catch(e) {
// Converting circular structure to JSON.
// We can't do anything, let's log the raw object.
}

console.warn("LOG-ERROR-DETAILS", all);
}

err.__alreadyLogged = true;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"dependencies": {
"async": "^0.9.0",
"opbeat": "^1.0.5",
"rarity": "^2.1.1",
"redis": "^0.12.1",
"restify": "^2.8.3",
Expand Down