From 411b510955ba8690c96c4a787338bc58a74db93a Mon Sep 17 00:00:00 2001 From: oof1lab Date: Wed, 1 Nov 2017 06:55:15 +0000 Subject: [PATCH] fix: error handling need more of this --- lib/xapi.js | 87 +++++++++++++++++++++++++++++--------------------- tests/tests.js | 25 +++++++++++++++ 2 files changed, 75 insertions(+), 37 deletions(-) diff --git a/lib/xapi.js b/lib/xapi.js index ba4befbfc6..4c5f04234b 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -69,10 +69,13 @@ class Xapi { if (err && err.code) res.status(400).json({error: err}); - else + else if (err && err.message) res.status(500).json({error: 'Internal server error : ' + err.message}); + else + res.status(500).json({error: 'Internal server error : ' + err}); next(err); + } asyncMiddleware(fn) { @@ -416,57 +419,67 @@ class Xapi { async groupBy(req, res) { - let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields; - let params = []; - let tableName = req.app.locals._tableName; + if (req.query && req.query._fields) { + let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields; + let params = []; + let tableName = req.app.locals._tableName; - params.push(tableName); + params.push(tableName); - if (!req.query.sort) { - req.query._sort = {} - req.query._sort = '-count' - } + if (!req.query.sort) { + req.query._sort = {} + req.query._sort = '-count' + } - query = query + this.mysql.getOrderByClause(req.query, tableName); + query = query + this.mysql.getOrderByClause(req.query, tableName); - var results = await this.mysql.exec(query, params); + var results = await this.mysql.exec(query, params); + + res.status(200).json(results); + } else { + res.status(400).json({message: 'Missing _fields query params eg: /api/tableName/groupby?_fields=column1'}) + } - res.status(200).json(results); } async aggregate(req, res) { - let tableName = req.app.locals._tableName; - let query = 'select ' - let params = [] - let fields = req.query._fields.split(','); - for (var i = 0; i < fields.length; ++i) { - if (i) { - query = query + ',' + if (req.query && req.query._fields) { + let tableName = req.app.locals._tableName; + let query = 'select ' + let params = [] + let fields = req.query._fields.split(','); + + for (var i = 0; i < fields.length; ++i) { + if (i) { + query = query + ',' + } + query = query + ' min(??) as ?,max(??) as ?,avg(??) as ?,sum(??) as ?,stddev(??) as ?,variance(??) as ? ' + params.push(fields[i]); + params.push('min_of_' + fields[i]); + params.push(fields[i]); + params.push('max_of_' + fields[i]); + params.push(fields[i]); + params.push('avg_of_' + fields[i]); + params.push(fields[i]); + params.push('sum_of_' + fields[i]); + params.push(fields[i]); + params.push('stddev_of_' + fields[i]); + params.push(fields[i]); + params.push('variance_of_' + fields[i]); } - query = query + ' min(??) as ?,max(??) as ?,avg(??) as ?,sum(??) as ?,stddev(??) as ?,variance(??) as ? ' - params.push(fields[i]); - params.push('min_of_' + fields[i]); - params.push(fields[i]); - params.push('max_of_' + fields[i]); - params.push(fields[i]); - params.push('avg_of_' + fields[i]); - params.push(fields[i]); - params.push('sum_of_' + fields[i]); - params.push(fields[i]); - params.push('stddev_of_' + fields[i]); - params.push(fields[i]); - params.push('variance_of_' + fields[i]); - } - query = query + ' from ??' - params.push(tableName) + query = query + ' from ??' + params.push(tableName) - 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'}); + } } diff --git a/tests/tests.js b/tests/tests.js index 2173fed8e8..f8a3fdbe75 100644 --- a/tests/tests.js +++ b/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) + }); + }); + + });