Commit eb6c14f4f20a79be1a72afe76376a141e2671430
1 parent
df6397d6
Exists in
master
and in
4 other branches
error handle of amqp manager
Showing
1 changed file
with
17 additions
and
19 deletions
Show diff stats
translate-api/helpers/amqpManager.js
@@ -13,20 +13,20 @@ var amqplib = require('amqplib/callback_api'); | @@ -13,20 +13,20 @@ var amqplib = require('amqplib/callback_api'); | ||
13 | /** | 13 | /** |
14 | * Function to send text to the queue. | 14 | * Function to send text to the queue. |
15 | */ | 15 | */ |
16 | -exports.sendToQueue = function(body, id, queue, durability, res) { | 16 | +exports.sendToQueue = function(body, id, queue, durability, res, next) { |
17 | 17 | ||
18 | amqplib.connect('amqp://localhost', function(err, conn) { | 18 | amqplib.connect('amqp://localhost', function(err, conn) { |
19 | - if(err) { | ||
20 | - res.json({message : err}); | ||
21 | - throw err; | ||
22 | - } | 19 | + if (err) |
20 | + return next(err, 'Cannot connect to RabbitMQ'); | ||
21 | + | ||
23 | conn.createChannel(function(err, ch) { | 22 | conn.createChannel(function(err, ch) { |
24 | - if(err) { | ||
25 | - res.json({message : err}); | ||
26 | - throw err; | ||
27 | - } | 23 | + if (err) |
24 | + return next(err, 'Channel creation failed.'); | ||
25 | + | ||
28 | ch.assertQueue(queue, {durable : durability}); | 26 | ch.assertQueue(queue, {durable : durability}); |
29 | ch.sendToQueue(queue, new Buffer(body), {correlationId : id}); | 27 | ch.sendToQueue(queue, new Buffer(body), {correlationId : id}); |
28 | + next(null, 'Message successfully sent'); | ||
29 | + | ||
30 | try { | 30 | try { |
31 | ch.close(); | 31 | ch.close(); |
32 | } | 32 | } |
@@ -41,23 +41,21 @@ exports.sendToQueue = function(body, id, queue, durability, res) { | @@ -41,23 +41,21 @@ exports.sendToQueue = function(body, id, queue, durability, res) { | ||
41 | /** | 41 | /** |
42 | * Function to receive gloss from the queue. | 42 | * Function to receive gloss from the queue. |
43 | */ | 43 | */ |
44 | -exports.receiveFromQueue = function(id, queue, durability, res) { | 44 | +exports.receiveFromQueue = function(id, queue, durability, res, next) { |
45 | 45 | ||
46 | amqplib.connect('amqp://localhost', function(err, conn) { | 46 | amqplib.connect('amqp://localhost', function(err, conn) { |
47 | - if(err) { | ||
48 | - res.json({message : err}); | ||
49 | - throw err; | ||
50 | - } | 47 | + if (err) |
48 | + return next(err, 'Cannot connect to RabbitMQ'); | ||
49 | + | ||
51 | conn.createChannel(function(err, ch) { | 50 | conn.createChannel(function(err, ch) { |
52 | - if(err){ | ||
53 | - res.json({message : err}); | ||
54 | - throw err; | ||
55 | - } | 51 | + if (err) |
52 | + return next(err, 'Channel creation failed.'); | ||
53 | + | ||
56 | ch.assertQueue(queue, {durable : durability}); | 54 | ch.assertQueue(queue, {durable : durability}); |
57 | ch.consume(queue, function(msg) { | 55 | ch.consume(queue, function(msg) { |
58 | if (msg.properties.correlationId === id) { | 56 | if (msg.properties.correlationId === id) { |
59 | ch.ack(msg); | 57 | ch.ack(msg); |
60 | - res.send(msg.content.toString()) | 58 | + next(null, msg.content.toString()) |
61 | try { | 59 | try { |
62 | ch.close(); | 60 | ch.close(); |
63 | } | 61 | } |