Browse Source

fix: error handling

need more of this
pull/8/head
oof1lab 7 years ago
parent
commit
411b510955
  1. 15
      lib/xapi.js
  2. 25
      tests/tests.js

15
lib/xapi.js

@ -69,10 +69,13 @@ class Xapi {
if (err && err.code) if (err && err.code)
res.status(400).json({error: err}); res.status(400).json({error: err});
else else if (err && err.message)
res.status(500).json({error: 'Internal server error : ' + err.message}); res.status(500).json({error: 'Internal server error : ' + err.message});
else
res.status(500).json({error: 'Internal server error : ' + err});
next(err); next(err);
} }
asyncMiddleware(fn) { asyncMiddleware(fn) {
@ -416,6 +419,7 @@ class Xapi {
async groupBy(req, res) { async groupBy(req, res) {
if (req.query && req.query._fields) {
let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields; let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields;
let params = []; let params = [];
let tableName = req.app.locals._tableName; let tableName = req.app.locals._tableName;
@ -432,11 +436,17 @@ class Xapi {
var results = await this.mysql.exec(query, params); var results = await this.mysql.exec(query, params);
res.status(200).json(results); res.status(200).json(results);
} else {
res.status(400).json({message: 'Missing _fields query params eg: /api/tableName/groupby?_fields=column1'})
}
} }
async aggregate(req, res) { async aggregate(req, res) {
if (req.query && req.query._fields) {
let tableName = req.app.locals._tableName; let tableName = req.app.locals._tableName;
let query = 'select ' let query = 'select '
let params = [] let params = []
@ -467,6 +477,9 @@ class Xapi {
var results = await this.mysql.exec(query, params); var results = await this.mysql.exec(query, params);
res.status(200).json(results); res.status(200).json(results);
} else {
res.status(400).json({message:'Missing _fields in query params eg: /api/tableName/groupby?_fields=numericColumn1'});
}
} }

25
tests/tests.js

@ -723,4 +723,29 @@ describe('xmysql : tests', function () {
}); });
it('GET /api/orders/aggregate should FAIL', function (done) {
//post to an url with data
agent.get('/api/orders/aggregate') //enter url
.expect(400)//200 for success 4xx for failure
.end(function (err, res) {
// Handle /api/v error
done(err)
});
});
it('GET /api/orders/groupby should FAIL', function (done) {
//post to an url with data
agent.get('/api/orders/groupby') //enter url
.expect(400)//200 for success 4xx for failure
.end(function (err, res) {
// Handle /api/v error
done(err)
});
});
}); });

Loading…
Cancel
Save