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

[Telescope#2755] Adding more tests for createError #63

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,75 @@ describe('hash', () => {
describe('Create Error tests for Satellite', () => {
test('should be an instance of type Error', () => {
expect(createError(404, 'testing') instanceof Error).toBe(true);

const testError = createError(404, 'Test for Errors');
const nestedError = createError(testError);
expect(nestedError instanceof Error).toBe(true);
});

test("should have it's value, and message accessible through it's members", () => {
const testError = createError(404, 'Satellite Test for Errors');
expect(testError.status).toBe(404);
expect(testError.message).toBe('Satellite Test for Errors');

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

test('should create an Error from an 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', () => {
// { errors } is imported from ElasticSearch
const elasticError = new errors.ResponseError({
body: { error: 'testing ElasticSearch Error' },
statusCode: 404,
});

try {
createError(503, elasticError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were bringing back a 503 because elastic returns 404 not available correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just replicating what's being done in the query.js that's causing the error

// query.js
router.get('/', validateQuery, async (req, res, next) => {
  try {
    const { text, filter, page, perPage } = req.query;
    res.send(await search(text, filter, page, perPage));
  } catch (error) {
    console.log({ error }, 'query error');
    next(createError(503, error));   // <-- crash is here
  }
});

If you look below in the indirect test, there are different ways we can send in the "correct" status code.
The thing is though, ES has different status code names for their errors than the regular http errors. Even if we send the ES status code in for createError, it'll just name it depending on the http error and not the ES error.

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

Expand Down