Browse Source

fix: error handling

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

87
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'});
}
}

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