Commit 974fcc3f0320010681d77a977e54f09e3063e400
1 parent
73b2d7d6
Exists in
master
and in
4 other branches
Translate api error handling
Showing
4 changed files
with
15 additions
and
21 deletions
Show diff stats
translate-api/app.js
@@ -55,7 +55,7 @@ app.use(function(err, req, res, next) { | @@ -55,7 +55,7 @@ app.use(function(err, req, res, next) { | ||
55 | if (app.get('env') !== 'development') | 55 | if (app.get('env') !== 'development') |
56 | delete err.stack | 56 | delete err.stack |
57 | res.status(err.status).json({ | 57 | res.status(err.status).json({ |
58 | - error: { | 58 | + 'error': { |
59 | 'message': err.message, | 59 | 'message': err.message, |
60 | 'status': err.status, | 60 | 'status': err.status, |
61 | 'stack': err.stack | 61 | 'stack': err.stack |
translate-api/controllers/translate.js
@@ -9,28 +9,23 @@ | @@ -9,28 +9,23 @@ | ||
9 | * Required libs. | 9 | * Required libs. |
10 | */ | 10 | */ |
11 | var shortid = require('shortid') | 11 | var shortid = require('shortid') |
12 | - , amqp = require('../helpers/amqpManager'); | 12 | + , amqp = require('../helpers/amqpManager') |
13 | + , error = require('../helpers/error'); | ||
13 | 14 | ||
14 | exports.translate = function(req, res) { | 15 | exports.translate = function(req, res) { |
15 | if (!req.body.text) | 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 | var id = shortid.generate(); | 19 | var id = shortid.generate(); |
19 | var text = req.body.text; | 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,7 +28,7 @@ exports.create = function(req, res, next) { | ||
28 | video.save(function(err) { | 28 | video.save(function(err) { |
29 | if (err) | 29 | if (err) |
30 | return error.internalError('A database error has occurred.', next); | 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 | resolve(gloss); | 32 | resolve(gloss); |
33 | }); | 33 | }); |
34 | }) | 34 | }) |
@@ -69,6 +69,6 @@ exports.status = function(req, res, next) { | @@ -69,6 +69,6 @@ exports.status = function(req, res, next) { | ||
69 | if (err) | 69 | if (err) |
70 | return error.notFound('Can\'t find any content.', next); | 70 | return error.notFound('Can\'t find any content.', next); |
71 | else | 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,7 +11,6 @@ var express = require('express') | ||
11 | * Routes to process text and return gloss. | 11 | * Routes to process text and return gloss. |
12 | */ | 12 | */ |
13 | router | 13 | router |
14 | - .get('/', translateController.urltranslate) | ||
15 | .post('/', translateController.translate) | 14 | .post('/', translateController.translate) |
16 | 15 | ||
17 | module.exports = router; | 16 | module.exports = router; |