CartoDB-SQL-API/test/unit/error-handler-factory-test.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-03-28 19:31:45 +08:00
'use strict';
const assert = require('assert');
const errorHandlerFactory = require('../../lib/services/error-handler-factory');
const ErrorHandler = require('../../lib/services/error-handler');
const { codeToCondition } = require('../../lib/postgresql/error-codes');
2018-03-28 19:31:45 +08:00
2019-12-24 01:19:08 +08:00
const rateLimitError = new Error(
2018-03-28 19:31:45 +08:00
'You are over platform\'s limits. Please contact us to know more details'
);
rateLimitError.http_status = 429;
rateLimitError.context = 'limit';
rateLimitError.detail = 'rate-limit';
const cases = [
{
title: 'postgres error',
error: new Error(codeToCondition['02000'])
},
{
title: 'rate limit error',
error: rateLimitError
}
];
describe('error-handler-factory', function () {
cases.forEach(({ title, error }) => {
it(title, function () {
const errorHandler = errorHandlerFactory(error);
2018-04-03 19:43:17 +08:00
const expectedError = new ErrorHandler({
message: error.message,
context: error.context,
detail: error.detail,
hint: error.hint,
2019-12-27 00:46:27 +08:00
httpStatus: error.http_status,
2018-04-03 19:43:17 +08:00
name: codeToCondition[error.code] || error.name
});
2018-03-28 19:31:45 +08:00
2019-12-26 21:01:18 +08:00
assert.deepStrictEqual(errorHandler, expectedError);
2018-03-28 19:31:45 +08:00
});
});
2019-12-24 01:19:08 +08:00
it('timeout error', function () {
2018-03-28 19:31:45 +08:00
const error = new Error('statement timeout');
const errorHandler = errorHandlerFactory(error);
2018-04-03 19:43:17 +08:00
const expectedError = new ErrorHandler({
2018-06-29 19:19:32 +08:00
message: 'You are over platform\'s limits: SQL query timeout error.' +
' Refactor your query before running again or contact CARTO support for more details.',
2018-04-03 19:43:17 +08:00
context: 'limit',
detail: 'datasource',
2019-12-27 00:46:27 +08:00
httpStatus: 429
2018-04-03 19:43:17 +08:00
});
2018-03-28 19:31:45 +08:00
2019-12-26 21:01:18 +08:00
assert.deepStrictEqual(errorHandler, expectedError);
2018-03-28 19:31:45 +08:00
});
2018-03-28 20:02:10 +08:00
2019-12-24 01:19:08 +08:00
it('permission denied error', function () {
2018-03-28 20:02:10 +08:00
const error = new Error('permission denied');
const errorHandler = errorHandlerFactory(error);
2018-04-03 19:43:17 +08:00
const expectedError = new ErrorHandler({
message: error.message,
context: error.context,
detail: error.detail,
hint: error.hint,
2019-12-27 00:46:27 +08:00
httpStatus: 403,
2018-04-03 19:43:17 +08:00
name: codeToCondition[error.code] || error.name
});
2018-03-28 20:02:10 +08:00
2019-12-26 21:01:18 +08:00
assert.deepStrictEqual(errorHandler, expectedError);
2018-03-28 20:02:10 +08:00
});
2018-03-28 19:31:45 +08:00
});