Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
adding ES error cases to createError
Browse files Browse the repository at this point in the history
  • Loading branch information
RC-Lee committed Feb 19, 2022
1 parent 127aba4 commit 95fb1e2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
22 changes: 22 additions & 0 deletions src/createError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const _createError = require('http-errors');
const { errors } = require('@elastic/elasticsearch');

module.exports.createError = (...args) => {
let status;
let argToSend;
let props = {};
for (let i = 0; i < args.length; i++) {
let arg = args[i];
let type = typeof arg;
if (type === 'object' && arg instanceof errors.ResponseError) {
argToSend = _createError(arg.statusCode, `ElasticSearch Error:${arg.name}`, arg.meta);
} else if (type === 'number' && i === 0) {
status = arg;
} else if (type === 'object' && !(arg instanceof Error)) {
props = arg;
} else {
argToSend = arg;
}
}
return _createError(status, argToSend, props);
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { createRouter } = require('./app');
module.exports.Satellite = require('./satellite');
module.exports.logger = require('./logger');
module.exports.hash = require('./hash');
module.exports.createError = require('http-errors');
module.exports.createError = require('./createError').createError;
module.exports.createServiceToken = require('./service-token');
module.exports.Router = (options) => createRouter(options);
module.exports.isAuthenticated = isAuthenticated;
Expand Down
51 changes: 11 additions & 40 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,64 +904,35 @@ describe('Create Error tests for Satellite', () => {
expect(testError.status).toBe(404);
expect(testError.message).toBe('Satellite Test for Errors');

const nestedError = createError(503, testError);
const prop = { key1: 'testing object', key2: 'for createError' };
const nestedError = createError(503, testError, prop);
// error status of an Error instance will not be overwritten
expect(nestedError.status).toBe(404);
expect(nestedError.message).toBe('Satellite Test for Errors');
expect(testError.key1).toBe('testing object');
expect(testError.key2).toBe('for createError');
});

test('should create an Error from an object', () => {
test('should create an Error from a non Error object', () => {
const testObj = { key1: 'testing object', key2: 'for createError' };
const testError = createError(404, testObj);

expect(testError instanceof Error).toBe(true);
expect(testError.key1).toBe('testing object');
expect(testError.key2).toBe('for createError');
});

test('should fail when directly creating Error from ElasticSearch error object', () => {
test('should create Error from ElasticSearch error object', () => {
// { errors } is imported from ElasticSearch
const elasticError = new errors.ResponseError({
body: { error: 'testing ElasticSearch Error' },
statusCode: 404,
});

try {
createError(503, elasticError);
} catch (err) {
expect(err instanceof TypeError).toBe(true);
expect(err.message).toBe(
'Cannot set property statusCode of [object Object] which has only a getter'
);
}
});

test('should create Error when indirectly creating from ElasticSearch error object', () => {
const elasticError = new errors.ResponseError({
body: {
errors: { error1: 'one ES error', error2: 'another ES error' },
status: 404,
error: 'test ElasticSearch Error',
},
statusCode: 404,
headers: {},
meta: {},
});

let testError = createError(503, elasticError.name, elasticError.body);

// Error status will be overwritten
expect(testError.status).toBe(503);
expect(testError.name).toBe('ServiceUnavailableError');
expect(testError.message).toBe('ResponseError');
expect(testError.errors).toStrictEqual({ error1: 'one ES error', error2: 'another ES error' });
expect(testError.error).toBe('test ElasticSearch Error');

testError = createError(elasticError.statusCode, elasticError.body);
expect(testError.status).toBe(404);
expect(testError.name).toBe('NotFoundError');
expect(testError.message).toBe('Not Found');
expect(testError.errors).toStrictEqual({ error1: 'one ES error', error2: 'another ES error' });
expect(testError.error).toBe('test ElasticSearch Error');
const testESError = createError(503, elasticError);
expect(testESError.status).toBe(404);
expect(testESError.message).toBe('ElasticSearch Error:ResponseError');
expect(testESError.body).toStrictEqual({ error: 'testing ElasticSearch Error' });
});
});

Expand Down

0 comments on commit 95fb1e2

Please sign in to comment.