Skip to content

Commit

Permalink
Swallows errors on ml lookups that we know might fail (elastic#25783) (
Browse files Browse the repository at this point in the history
…elastic#25844)

* Swallows errors on ml lookups that we know can fail

* Adjusts when we swallow ml lookup errors, fixes async test
  • Loading branch information
jasonrhodes authored Nov 17, 2018
1 parent 7cfb3e3 commit 5cd1f9b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

// @ts-ignore
import { getAnomalyAggs } from '../get_anomaly_aggs';

test('getAnomalyAggs should swallow HTTP errors', () => {
const httpError = new Error('anomaly lookup failed') as any;
httpError.statusCode = 418;
const failClient = jest.fn(() => Promise.reject(httpError));

return expect(getAnomalyAggs({ client: failClient })).resolves.toEqual(null);
});

test('getAnomalyAggs should throw other errors', () => {
const otherError = new Error('anomaly lookup ASPLODED') as any;
const failClient = jest.fn(() => Promise.reject(otherError));

return expect(
getAnomalyAggs({
client: failClient
})
).rejects.toThrow(otherError);
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export async function getAnomalyAggs({
try {
const resp = await client('search', params);
return resp.aggregations;
} catch (e) {
if (e.statusCode === 404) {
} catch (err) {
if ('statusCode' in err) {
// swallow HTTP errors because there are lots of reasons
// the ml index lookup may fail, and we're ok with that
return null;
}
throw e;

throw err;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function getAvgResponseTimeAnomalies({

if (!aggs) {
return {
message: 'ml index does not exist'
message: 'Error reading machine learning index'
};
}

Expand Down

0 comments on commit 5cd1f9b

Please sign in to comment.