Commit 73b2d7d65664e8554f9826abd609f973524323d0
1 parent
5033e81f
Exists in
master
and in
4 other branches
Api error handling
Showing
5 changed files
with
58 additions
and
17 deletions
Show diff stats
translate-api/app.js
... | ... | @@ -52,8 +52,15 @@ app.use('/video', video); |
52 | 52 | * Error handler. |
53 | 53 | */ |
54 | 54 | app.use(function(err, req, res, next) { |
55 | - res.status(err.status || 500); | |
56 | - res.json({ message: err.message, error: err }); | |
55 | + if (app.get('env') !== 'development') | |
56 | + delete err.stack | |
57 | + res.status(err.status).json({ | |
58 | + error: { | |
59 | + 'message': err.message, | |
60 | + 'status': err.status, | |
61 | + 'stack': err.stack | |
62 | + } | |
63 | + }); | |
57 | 64 | }); |
58 | 65 | |
59 | 66 | module.exports = app; | ... | ... |
translate-api/config/db.js
1 | 1 | /** |
2 | - * Author: Laércio S. Bezerra | |
3 | - * Email: laerciosouza@lavid.ufpb.br | |
2 | + * Author: Wesnydy L. Ribeiro | |
3 | + * Email: wesnydy@lavid.ufpb.br | |
4 | 4 | */ |
5 | 5 | |
6 | 6 | /* |
... | ... | @@ -9,13 +9,10 @@ |
9 | 9 | 'use strict'; |
10 | 10 | |
11 | 11 | /* |
12 | - * Required Lib. | |
12 | + * Required Libs. | |
13 | 13 | */ |
14 | 14 | var mongoose = require('mongoose'); |
15 | 15 | |
16 | -/* | |
17 | - * My Lib for javascript Promises | |
18 | - */ | |
19 | 16 | mongoose.Promise = require('bluebird'); |
20 | 17 | |
21 | 18 | /* | ... | ... |
translate-api/controllers/video.js
... | ... | @@ -10,11 +10,12 @@ |
10 | 10 | */ |
11 | 11 | var shortid = require('shortid') |
12 | 12 | , Video = require('../models/video') |
13 | - , amqp = require('../helpers/amqpManager'); | |
13 | + , amqp = require('../helpers/amqpManager') | |
14 | + , error = require('../helpers/error'); | |
14 | 15 | |
15 | -exports.create = function(req, res) { | |
16 | +exports.create = function(req, res, next) { | |
16 | 17 | if (!req.body.gloss) |
17 | - return console.log('Gloss key missing'); | |
18 | + return error.badRequest('The gloss key is missing.', next); | |
18 | 19 | |
19 | 20 | var id = shortid.generate(); |
20 | 21 | var gloss = req.body.gloss; |
... | ... | @@ -26,7 +27,7 @@ exports.create = function(req, res) { |
26 | 27 | new Promise(function (resolve) { |
27 | 28 | video.save(function(err) { |
28 | 29 | if (err) |
29 | - return console.error(err); | |
30 | + return error.internalError('A database error has occurred.', next); | |
30 | 31 | res.json({ id: video._id }); |
31 | 32 | resolve(gloss); |
32 | 33 | }); |
... | ... | @@ -35,10 +36,10 @@ exports.create = function(req, res) { |
35 | 36 | return new Promise(function(resolve) { |
36 | 37 | amqp.sendToQueue(gloss, id, 'glosses', false, res, function(err) { |
37 | 38 | if (err) |
38 | - return console.error(err); | |
39 | + return error.internalError('An internal communication error has occurred.', next); | |
39 | 40 | amqp.receiveFromQueue(id, 'videos', false, res, function(err, message) { |
40 | 41 | if (err) |
41 | - return console.error(err); | |
42 | + return error.internalError('An internal communication error has occurred.', next); | |
42 | 43 | resolve(message); |
43 | 44 | }); |
44 | 45 | }); |
... | ... | @@ -53,20 +54,20 @@ exports.create = function(req, res) { |
53 | 54 | } |
54 | 55 | }, {new: true}, function(err, content) { |
55 | 56 | if (err) |
56 | - return console.log(err); | |
57 | + return error.internalError('A database error has occurred.', next); | |
57 | 58 | }); |
58 | 59 | }); |
59 | 60 | }); |
60 | 61 | }; |
61 | 62 | |
62 | -exports.status = function(req, res) { | |
63 | +exports.status = function(req, res, next) { | |
63 | 64 | // Receive param id |
64 | 65 | var contentID = req.params.id; |
65 | 66 | // Find content by id on MongoDB |
66 | 67 | return Video.findById(contentID, function (err, video) { |
67 | 68 | // returns error when can not find content |
68 | 69 | if (err) |
69 | - res.status(404).send('Not Found'); | |
70 | + return error.notFound('Can\'t find any content.', next); | |
70 | 71 | else |
71 | 72 | res.json( { 'status': video.status, 'file': video.file, 'size': video.size } ); |
72 | 73 | }); | ... | ... |
translate-api/helpers/amqpManager.js
... | ... | @@ -0,0 +1,33 @@ |
1 | +/** | |
2 | + * Author: Wesnydy Lima Ribeiro | |
3 | + * Email: wesnydy@lavid.ufpb.br | |
4 | + */ | |
5 | + | |
6 | +'use strict'; | |
7 | + | |
8 | +/** | |
9 | + * Error when not find the resource. | |
10 | + */ | |
11 | +exports.notFound = function(message, next) { | |
12 | + var err = new Error(message || 'The requested resource couldn\'t be found.'); | |
13 | + err.status = 404; | |
14 | + next(err); | |
15 | +}; | |
16 | + | |
17 | +/** | |
18 | + * Error when receive a malformed request syntax. | |
19 | + */ | |
20 | +exports.badRequest = function(message, next) { | |
21 | + var err = new Error(message || 'The Request can\'t be fulfilled due to bad syntax.'); | |
22 | + err.status = 400; | |
23 | + next(err); | |
24 | +}; | |
25 | + | |
26 | +/** | |
27 | + * Error when workers processing. | |
28 | + */ | |
29 | +exports.internalError = function(message, next) { | |
30 | + var err = new Error(message || 'An internal error occurred during processing.'); | |
31 | + err.status = 500; | |
32 | + next(err); | |
33 | +}; | ... | ... |