Commit 974fcc3f0320010681d77a977e54f09e3063e400

Authored by Wesnydy Ribeiro
1 parent 73b2d7d6

Translate api error handling

translate-api/app.js
... ... @@ -55,7 +55,7 @@ app.use(function(err, req, res, next) {
55 55 if (app.get('env') !== 'development')
56 56 delete err.stack
57 57 res.status(err.status).json({
58   - error: {
  58 + 'error': {
59 59 'message': err.message,
60 60 'status': err.status,
61 61 'stack': err.stack
... ...
translate-api/controllers/translate.js
... ... @@ -9,28 +9,23 @@
9 9 * Required libs.
10 10 */
11 11 var shortid = require('shortid')
12   - , amqp = require('../helpers/amqpManager');
  12 + , amqp = require('../helpers/amqpManager')
  13 + , error = require('../helpers/error');
13 14  
14 15 exports.translate = function(req, res) {
15 16 if (!req.body.text)
16   - return console.log('Text key is missing');
  17 + return error.badRequest('The text key is missing.', next);
17 18  
18 19 var id = shortid.generate();
19 20 var text = req.body.text;
20 21  
21   - amqp.sendToQueue(text, id,'texts', false, res);
22   - amqp.sendToQueue(text, id,'logs', true, res);
23   - amqp.receiveFromQueue(id, 'translations', false, res);
24   -};
25   -
26   -exports.urltranslate = function(req, res) {
27   - if (!req.body.text)
28   - return console.log('Text key missing');
29   -
30   - var id = shortid.generate();
31   - var text = req.param('text').toString('utf8');
32   -
33   - amqp.sendToQueue(text, id, 'texts', false, res);
34   - amqp.sendToQueue(text, id, 'logs', true, res);
35   - amqp.receiveFromQueue(id, 'translations', false, res);
  22 + amqp.sendToQueue(text, id,'texts', false, res, function(err) {
  23 + if (err)
  24 + return error.internalError('An internal communication error has occurred.', next);
  25 + amqp.receiveFromQueue(id, 'translations', false, res, function(err, message) {
  26 + if (err)
  27 + return error.internalError('An internal communication error has occurred.', next);
  28 + res.status(200).send(message);
  29 + });
  30 + });
36 31 };
... ...
translate-api/controllers/video.js
... ... @@ -28,7 +28,7 @@ exports.create = function(req, res, next) {
28 28 video.save(function(err) {
29 29 if (err)
30 30 return error.internalError('A database error has occurred.', next);
31   - res.json({ id: video._id });
  31 + res.status(200).json({ id: video._id });
32 32 resolve(gloss);
33 33 });
34 34 })
... ... @@ -69,6 +69,6 @@ exports.status = function(req, res, next) {
69 69 if (err)
70 70 return error.notFound('Can\'t find any content.', next);
71 71 else
72   - res.json( { 'status': video.status, 'file': video.file, 'size': video.size } );
  72 + res.status(200).json({ 'status': video.status, 'file': video.file, 'size': video.size });
73 73 });
74 74 };
... ...
translate-api/routes/translate.js
... ... @@ -11,7 +11,6 @@ var express = require('express')
11 11 * Routes to process text and return gloss.
12 12 */
13 13 router
14   - .get('/', translateController.urltranslate)
15 14 .post('/', translateController.translate)
16 15  
17 16 module.exports = router;
... ...