From eb6c14f4f20a79be1a72afe76376a141e2671430 Mon Sep 17 00:00:00 2001 From: Wesnydy Ribeiro Date: Fri, 24 Feb 2017 10:02:25 -0300 Subject: [PATCH] error handle of amqp manager --- translate-api/helpers/amqpManager.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/translate-api/helpers/amqpManager.js b/translate-api/helpers/amqpManager.js index 57b8a52..f25362b 100644 --- a/translate-api/helpers/amqpManager.js +++ b/translate-api/helpers/amqpManager.js @@ -13,20 +13,20 @@ var amqplib = require('amqplib/callback_api'); /** * Function to send text to the queue. */ -exports.sendToQueue = function(body, id, queue, durability, res) { +exports.sendToQueue = function(body, id, queue, durability, res, next) { amqplib.connect('amqp://localhost', function(err, conn) { - if(err) { - res.json({message : err}); - throw err; - } + if (err) + return next(err, 'Cannot connect to RabbitMQ'); + conn.createChannel(function(err, ch) { - if(err) { - res.json({message : err}); - throw err; - } + if (err) + return next(err, 'Channel creation failed.'); + ch.assertQueue(queue, {durable : durability}); ch.sendToQueue(queue, new Buffer(body), {correlationId : id}); + next(null, 'Message successfully sent'); + try { ch.close(); } @@ -41,23 +41,21 @@ exports.sendToQueue = function(body, id, queue, durability, res) { /** * Function to receive gloss from the queue. */ -exports.receiveFromQueue = function(id, queue, durability, res) { +exports.receiveFromQueue = function(id, queue, durability, res, next) { amqplib.connect('amqp://localhost', function(err, conn) { - if(err) { - res.json({message : err}); - throw err; - } + if (err) + return next(err, 'Cannot connect to RabbitMQ'); + conn.createChannel(function(err, ch) { - if(err){ - res.json({message : err}); - throw err; - } + if (err) + return next(err, 'Channel creation failed.'); + ch.assertQueue(queue, {durable : durability}); ch.consume(queue, function(msg) { if (msg.properties.correlationId === id) { ch.ack(msg); - res.send(msg.content.toString()) + next(null, msg.content.toString()) try { ch.close(); } -- libgit2 0.21.2