2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 17:29:07 +08:00
|
|
|
require('../support/test-helper');
|
2015-09-17 03:54:56 +08:00
|
|
|
|
|
|
|
var assert = require('assert');
|
2019-10-07 17:29:07 +08:00
|
|
|
var errorMiddleware = require('../../lib/api/middlewares/error-middleware');
|
2015-09-17 03:54:56 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('error-middleware', function () {
|
|
|
|
it('different formats for postgis plugin error returns 400 as status code', function () {
|
2015-09-17 03:54:56 +08:00
|
|
|
var expectedStatusCode = 400;
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(
|
2019-10-22 01:07:24 +08:00
|
|
|
errorMiddleware.findStatusCode('Postgis Plugin: ERROR: column "missing" does not exist\n'),
|
2015-09-17 03:54:56 +08:00
|
|
|
expectedStatusCode,
|
2019-10-22 01:07:24 +08:00
|
|
|
'Error status code for single line does not match'
|
2015-09-17 03:54:56 +08:00
|
|
|
);
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(
|
2019-10-22 01:07:24 +08:00
|
|
|
errorMiddleware.findStatusCode('Postgis Plugin: PSQL error:\nERROR: column "missing" does not exist\n'),
|
2015-09-17 03:54:56 +08:00
|
|
|
expectedStatusCode,
|
2019-10-22 01:07:24 +08:00
|
|
|
'Error status code for multiline/PSQL does not match'
|
2015-09-17 03:54:56 +08:00
|
|
|
);
|
|
|
|
});
|
2017-11-28 01:14:02 +08:00
|
|
|
|
|
|
|
it('should return a header with errors', function (done) {
|
|
|
|
const error = new Error('error test');
|
2017-11-28 01:43:48 +08:00
|
|
|
error.label = 'test label';
|
|
|
|
error.type = 'test type';
|
|
|
|
error.subtype = 'test subtype';
|
2017-11-28 01:14:02 +08:00
|
|
|
|
2017-11-28 01:43:48 +08:00
|
|
|
const errors = [error, error];
|
2018-04-07 00:20:33 +08:00
|
|
|
|
2017-11-28 01:14:02 +08:00
|
|
|
const req = {};
|
|
|
|
const res = {
|
|
|
|
headers: {},
|
|
|
|
set (key, value) {
|
|
|
|
this.headers[key] = value;
|
|
|
|
},
|
|
|
|
statusCode: 0,
|
|
|
|
status (status) {
|
|
|
|
this.statusCode = status;
|
|
|
|
},
|
|
|
|
json () {},
|
|
|
|
send () {}
|
|
|
|
};
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
const errorHeader = {
|
2017-11-29 01:22:55 +08:00
|
|
|
mainError: {
|
|
|
|
statusCode: 400,
|
|
|
|
message: error.message,
|
|
|
|
name: error.name,
|
|
|
|
label: error.label,
|
|
|
|
type: error.type,
|
2019-10-22 01:07:24 +08:00
|
|
|
subtype: error.subtype
|
2017-11-29 01:22:55 +08:00
|
|
|
},
|
2017-11-28 01:43:48 +08:00
|
|
|
moreErrors: [{
|
|
|
|
message: error.message,
|
|
|
|
name: error.name,
|
|
|
|
label: error.label,
|
|
|
|
type: error.type,
|
|
|
|
subtype: error.subtype
|
|
|
|
}]
|
2017-11-28 01:14:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const errorFn = errorMiddleware();
|
2017-11-28 01:43:48 +08:00
|
|
|
errorFn(errors, req, res);
|
2017-11-28 01:14:02 +08:00
|
|
|
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(res.headers, {
|
2017-11-28 01:14:02 +08:00
|
|
|
'X-Tiler-Errors': JSON.stringify(errorHeader)
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-11-29 00:27:05 +08:00
|
|
|
|
|
|
|
it('JSONP should return a header with error status code', function (done) {
|
|
|
|
const error = new Error('error test');
|
|
|
|
error.label = 'test label';
|
|
|
|
error.type = 'test type';
|
|
|
|
error.subtype = 'test subtype';
|
|
|
|
|
|
|
|
const errors = [error, error];
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
query: { callback: true }
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
headers: {},
|
|
|
|
set (key, value) {
|
|
|
|
this.headers[key] = value;
|
|
|
|
},
|
|
|
|
statusCode: 0,
|
|
|
|
status (status) {
|
|
|
|
this.statusCode = status;
|
|
|
|
},
|
|
|
|
jsonp () {},
|
|
|
|
send () {}
|
|
|
|
};
|
|
|
|
|
|
|
|
const errorHeader = {
|
2017-11-29 01:22:55 +08:00
|
|
|
mainError: {
|
|
|
|
statusCode: 400,
|
|
|
|
message: error.message,
|
|
|
|
name: error.name,
|
|
|
|
label: error.label,
|
|
|
|
type: error.type,
|
2019-10-22 01:07:24 +08:00
|
|
|
subtype: error.subtype
|
2017-11-29 01:22:55 +08:00
|
|
|
},
|
2017-11-29 00:27:05 +08:00
|
|
|
moreErrors: [{
|
|
|
|
message: error.message,
|
|
|
|
name: error.name,
|
|
|
|
label: error.label,
|
|
|
|
type: error.type,
|
|
|
|
subtype: error.subtype
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const errorFn = errorMiddleware();
|
|
|
|
errorFn(errors, req, res);
|
|
|
|
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(res.headers, {
|
2017-11-29 00:27:05 +08:00
|
|
|
'X-Tiler-Errors': JSON.stringify(errorHeader)
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-12-18 19:35:44 +08:00
|
|
|
|
|
|
|
it('should escape chars that broke logs regex', function (done) {
|
2019-10-25 01:18:47 +08:00
|
|
|
const badString = 'error: ( ) = " \" \' * $ & |'; // eslint-disable-line no-useless-escape
|
2017-12-18 19:35:44 +08:00
|
|
|
const escapedString = 'error ';
|
|
|
|
|
|
|
|
const error = new Error(badString);
|
|
|
|
error.label = badString;
|
|
|
|
error.type = badString;
|
|
|
|
error.subtype = badString;
|
|
|
|
|
|
|
|
const errors = [error, error];
|
2018-04-07 00:20:33 +08:00
|
|
|
|
2017-12-18 19:35:44 +08:00
|
|
|
const req = {};
|
|
|
|
const res = {
|
|
|
|
headers: {},
|
|
|
|
set (key, value) {
|
|
|
|
this.headers[key] = value;
|
|
|
|
},
|
|
|
|
statusCode: 0,
|
|
|
|
status (status) {
|
|
|
|
this.statusCode = status;
|
|
|
|
},
|
|
|
|
json () {},
|
|
|
|
send () {}
|
|
|
|
};
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
const errorHeader = {
|
2017-12-18 19:35:44 +08:00
|
|
|
mainError: {
|
|
|
|
statusCode: 400,
|
|
|
|
message: escapedString,
|
|
|
|
name: error.name,
|
|
|
|
label: escapedString,
|
|
|
|
type: escapedString,
|
2019-10-22 01:07:24 +08:00
|
|
|
subtype: escapedString
|
2017-12-18 19:35:44 +08:00
|
|
|
},
|
|
|
|
moreErrors: [{
|
|
|
|
message: escapedString,
|
|
|
|
name: error.name,
|
|
|
|
label: escapedString,
|
|
|
|
type: escapedString,
|
|
|
|
subtype: escapedString
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const errorFn = errorMiddleware();
|
|
|
|
errorFn(errors, req, res);
|
|
|
|
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(res.headers, {
|
2017-12-18 19:35:44 +08:00
|
|
|
'X-Tiler-Errors': JSON.stringify(errorHeader)
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2015-09-17 03:54:56 +08:00
|
|
|
});
|