From ff4245d9652974eb7718f9a00888fc8a08688e12 Mon Sep 17 00:00:00 2001 From: Zalmoxisus Date: Wed, 17 Aug 2016 18:31:12 +0300 Subject: [PATCH] Return error in request --- lib/store.js | 11 +++++++++++ lib/worker.js | 12 ++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/store.js b/lib/store.js index 65bab62..49c6a9a 100644 --- a/lib/store.js +++ b/lib/store.js @@ -5,12 +5,23 @@ var store; var adapter; var Report; +function error(msg) { + return new Promise(function(resolve, reject) { + return resolve({ error: msg }); + }); +} + function add(data) { + if (!data.type || !data.payload) { + return error('Required parameters aren\'t specified.'); + } + var obj = { id: uuid.v4(), type: data.type, payload: data.payload }; + if (!adapter) { return new Promise(function(resolve) { var report = Report.inject(obj); diff --git a/lib/worker.js b/lib/worker.js index a2aea54..3eccacc 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -23,10 +23,14 @@ module.exports.run = function(worker) { app.post('/', function(req, res) { if (!req.body) return res.status(404).end(); if (!store) store = createStore(worker.options); - store.add(req.body).then(function(r) { - scServer.exchange.publish('log', req.body); - res.send({ id: r.id }); - }); + + switch(req.body.op) { + default: + store.add(req.body).then(function(r) { + scServer.exchange.publish('log', req.body); + res.send({ id: r.id, error: r.error }); + }); + } }); scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {