improve error handling with rpc response API
This commit is contained in:
parent
cb53bd1c15
commit
a4c8039cc7
@ -772,7 +772,9 @@ HK.initializeIndexWorkers = function (Env, config, _cb) {
|
|||||||
|
|
||||||
const workers = [];
|
const workers = [];
|
||||||
|
|
||||||
const response = Util.response();
|
const response = Util.response(function (errLabel, info) {
|
||||||
|
Env.Log.error('HK_DB_WORKER__' + errLabel, info);
|
||||||
|
});
|
||||||
const initWorker = function (worker, cb) {
|
const initWorker = function (worker, cb) {
|
||||||
//console.log("initializing index worker");
|
//console.log("initializing index worker");
|
||||||
const txid = Util.uid();
|
const txid = Util.uid();
|
||||||
@ -798,14 +800,7 @@ HK.initializeIndexWorkers = function (Env, config, _cb) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//console.log(res);
|
//console.log(res);
|
||||||
try {
|
|
||||||
response.handle(res.txid, [res.error, res.value]);
|
response.handle(res.txid, [res.error, res.value]);
|
||||||
} catch (err) {
|
|
||||||
Env.Log.error("INDEX_WORKER", {
|
|
||||||
error: err,
|
|
||||||
response: res,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
worker.on('exit', function () {
|
worker.on('exit', function () {
|
||||||
var idx = workers.indexOf(worker);
|
var idx = workers.indexOf(worker);
|
||||||
@ -945,7 +940,9 @@ HK.initializeValidationWorkers = function (Env) {
|
|||||||
workers.push(fork('lib/workers/check-signature.js'));
|
workers.push(fork('lib/workers/check-signature.js'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = Util.response();
|
const response = Util.response(function (errLabel, info) {
|
||||||
|
Env.Log.error('HK_VALIDATE_WORKER__' + errLabel, info);
|
||||||
|
});
|
||||||
|
|
||||||
var initWorker = function (worker) {
|
var initWorker = function (worker) {
|
||||||
worker.on('message', function (res) {
|
worker.on('message', function (res) {
|
||||||
|
|||||||
@ -81,10 +81,16 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Util.response = function () {
|
Util.response = function (errorHandler) {
|
||||||
var pending = {};
|
var pending = {};
|
||||||
var timeouts = {};
|
var timeouts = {};
|
||||||
|
|
||||||
|
if (typeof(errorHandler) !== 'function') {
|
||||||
|
errorHandler = function (label) {
|
||||||
|
throw new Error(label);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var clear = function (id) {
|
var clear = function (id) {
|
||||||
clearTimeout(timeouts[id]);
|
clearTimeout(timeouts[id]);
|
||||||
delete timeouts[id];
|
delete timeouts[id];
|
||||||
@ -92,8 +98,8 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
var expect = function (id, fn, ms) {
|
var expect = function (id, fn, ms) {
|
||||||
if (typeof(id) !== 'string') { throw new Error("EXPECTED_STRING"); }
|
if (typeof(id) !== 'string') { errorHandler('EXPECTED_STRING'); }
|
||||||
if (typeof(fn) !== 'function') { throw new Error("EXPECTED_CALLBACK"); }
|
if (typeof(fn) !== 'function') { errorHandler('EXPECTED_CALLBACK'); }
|
||||||
pending[id] = fn;
|
pending[id] = fn;
|
||||||
if (typeof(ms) === 'number' && ms) {
|
if (typeof(ms) === 'number' && ms) {
|
||||||
timeouts[id] = setTimeout(function () {
|
timeouts[id] = setTimeout(function () {
|
||||||
@ -105,8 +111,21 @@
|
|||||||
|
|
||||||
var handle = function (id, args) {
|
var handle = function (id, args) {
|
||||||
var fn = pending[id];
|
var fn = pending[id];
|
||||||
if (typeof(fn) !== 'function') { throw new Error("MISSING_CALLBACK"); }
|
if (typeof(fn) !== 'function') {
|
||||||
|
errorHandler("MISSING_CALLBACK", {
|
||||||
|
id: id,
|
||||||
|
args: args,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
try {
|
||||||
pending[id].apply(null, Array.isArray(args)? args : [args]);
|
pending[id].apply(null, Array.isArray(args)? args : [args]);
|
||||||
|
} catch (err) {
|
||||||
|
errorHandler('HANDLER_ERROR', {
|
||||||
|
error: err,
|
||||||
|
id: id,
|
||||||
|
args: args,
|
||||||
|
});
|
||||||
|
}
|
||||||
clear(id);
|
clear(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user