2017-11-29 00:19:48 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var assert = require('assert');
|
2019-10-04 00:24:39 +08:00
|
|
|
var errorMiddleware = require('../../lib/api/middlewares/error');
|
2018-12-05 23:22:48 +08:00
|
|
|
require('../helper');
|
2017-11-29 00:19:48 +08:00
|
|
|
|
2018-03-28 18:10:48 +08:00
|
|
|
const req = { query: { callback: true } };
|
|
|
|
|
|
|
|
const getRes = () => {
|
|
|
|
return {
|
|
|
|
headers: {},
|
|
|
|
set (key, value) {
|
|
|
|
this.headers[key] = value;
|
|
|
|
},
|
|
|
|
header (key, value) {
|
|
|
|
this.set(key, value);
|
|
|
|
},
|
|
|
|
statusCode: 0,
|
|
|
|
status (status) {
|
|
|
|
this.statusCode = status;
|
|
|
|
},
|
|
|
|
json () {},
|
|
|
|
jsonp () {}
|
2018-03-28 18:17:42 +08:00
|
|
|
};
|
2018-03-28 18:10:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const getErrorHeader = (context, detail, hint, message) => {
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
detail,
|
|
|
|
hint,
|
|
|
|
statusCode: 400,
|
|
|
|
message
|
2018-03-28 18:17:42 +08:00
|
|
|
};
|
2018-03-28 18:10:48 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('error-handler', function () {
|
2017-11-29 00:19:48 +08:00
|
|
|
it('should return a header with errors', function (done) {
|
2019-12-24 01:19:08 +08:00
|
|
|
const error = new Error('error test');
|
2017-11-29 00:19:48 +08:00
|
|
|
error.detail = 'test detail';
|
|
|
|
error.hint = 'test hint';
|
|
|
|
error.context = 'test context';
|
|
|
|
|
2018-03-28 18:10:48 +08:00
|
|
|
const errorHeader = getErrorHeader(
|
|
|
|
error.context,
|
|
|
|
error.detail,
|
|
|
|
error.hint,
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
|
|
|
|
const res = getRes();
|
2017-11-29 00:19:48 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
errorMiddleware()(error, req, res, function next () {
|
|
|
|
assert.ok(res.headers['X-SQLAPI-Errors'].length > 0);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-02-17 01:21:06 +08:00
|
|
|
res.headers['X-SQLAPI-Errors'],
|
|
|
|
JSON.stringify(errorHeader)
|
|
|
|
);
|
2017-11-29 00:19:48 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
done();
|
|
|
|
});
|
2017-11-29 00:19:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('JSONP should return a header with error statuscode', function (done) {
|
2019-12-24 01:19:08 +08:00
|
|
|
const error = new Error('error test');
|
2017-11-29 00:19:48 +08:00
|
|
|
error.detail = 'test detail';
|
|
|
|
error.hint = 'test hint';
|
|
|
|
error.context = 'test context';
|
|
|
|
|
2018-03-28 18:10:48 +08:00
|
|
|
const errorHeader = getErrorHeader(
|
|
|
|
error.context,
|
|
|
|
error.detail,
|
|
|
|
error.hint,
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const res = getRes();
|
2017-11-29 00:19:48 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
errorMiddleware()(error, req, res, function next () {
|
|
|
|
assert.ok(res.headers['X-SQLAPI-Errors'].length > 0);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-02-17 01:21:06 +08:00
|
|
|
res.headers['X-SQLAPI-Errors'],
|
|
|
|
JSON.stringify(errorHeader)
|
|
|
|
);
|
2017-11-29 00:19:48 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
done();
|
|
|
|
});
|
2017-11-29 00:19:48 +08:00
|
|
|
});
|
2017-12-18 19:46:03 +08:00
|
|
|
|
|
|
|
it('should escape chars that broke logs regex', function (done) {
|
|
|
|
const badString = 'error: ( ) = " \" \' * $ & |';
|
|
|
|
const escapedString = 'error ';
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
const error = new Error(badString);
|
2017-12-18 19:46:03 +08:00
|
|
|
error.detail = badString;
|
|
|
|
error.hint = badString;
|
|
|
|
error.context = badString;
|
|
|
|
|
2018-03-28 18:10:48 +08:00
|
|
|
const errorHeader = getErrorHeader(
|
|
|
|
escapedString,
|
|
|
|
escapedString,
|
|
|
|
escapedString,
|
|
|
|
escapedString
|
|
|
|
);
|
2017-12-18 19:46:03 +08:00
|
|
|
|
2018-03-28 18:10:48 +08:00
|
|
|
const res = getRes();
|
2019-10-04 00:24:39 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
errorMiddleware()(error, req, res, function () {
|
|
|
|
assert.ok(res.headers['X-SQLAPI-Errors'].length > 0);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-02-17 01:21:06 +08:00
|
|
|
res.headers['X-SQLAPI-Errors'],
|
|
|
|
JSON.stringify(errorHeader)
|
|
|
|
);
|
2017-12-18 19:46:03 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
done();
|
|
|
|
});
|
2017-12-18 19:46:03 +08:00
|
|
|
});
|
2018-12-06 01:59:11 +08:00
|
|
|
|
2018-12-06 02:15:57 +08:00
|
|
|
it('should truncat too long error messages', function (done) {
|
|
|
|
const veryLongString = 'Very long error message '.repeat(1000);
|
|
|
|
const truncatedString = veryLongString.substring(0, 1024);
|
2018-12-06 01:59:11 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
const error = new Error(veryLongString);
|
2018-12-06 01:59:11 +08:00
|
|
|
|
|
|
|
const expectedErrorHeader = {
|
|
|
|
statusCode: 400,
|
|
|
|
message: truncatedString
|
2019-12-24 01:19:08 +08:00
|
|
|
};
|
2018-12-06 01:59:11 +08:00
|
|
|
|
|
|
|
const res = getRes();
|
|
|
|
|
|
|
|
errorMiddleware()(error, req, res, function () {
|
|
|
|
assert.ok(res.headers['X-SQLAPI-Errors'].length > 0);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(
|
2018-12-06 01:59:11 +08:00
|
|
|
res.headers['X-SQLAPI-Errors'],
|
|
|
|
JSON.stringify(expectedErrorHeader)
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-11-29 00:19:48 +08:00
|
|
|
});
|