Return error in request
This commit is contained in:
11
lib/store.js
11
lib/store.js
@ -5,12 +5,23 @@ var store;
|
|||||||
var adapter;
|
var adapter;
|
||||||
var Report;
|
var Report;
|
||||||
|
|
||||||
|
function error(msg) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
return resolve({ error: msg });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function add(data) {
|
function add(data) {
|
||||||
|
if (!data.type || !data.payload) {
|
||||||
|
return error('Required parameters aren\'t specified.');
|
||||||
|
}
|
||||||
|
|
||||||
var obj = {
|
var obj = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
type: data.type,
|
type: data.type,
|
||||||
payload: data.payload
|
payload: data.payload
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!adapter) {
|
if (!adapter) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var report = Report.inject(obj);
|
var report = Report.inject(obj);
|
||||||
|
@ -23,10 +23,14 @@ module.exports.run = function(worker) {
|
|||||||
app.post('/', function(req, res) {
|
app.post('/', function(req, res) {
|
||||||
if (!req.body) return res.status(404).end();
|
if (!req.body) return res.status(404).end();
|
||||||
if (!store) store = createStore(worker.options);
|
if (!store) store = createStore(worker.options);
|
||||||
|
|
||||||
|
switch(req.body.op) {
|
||||||
|
default:
|
||||||
store.add(req.body).then(function(r) {
|
store.add(req.body).then(function(r) {
|
||||||
scServer.exchange.publish('log', req.body);
|
scServer.exchange.publish('log', req.body);
|
||||||
res.send({ id: r.id });
|
res.send({ id: r.id, error: r.error });
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
|
scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
|
||||||
|
Reference in New Issue
Block a user