diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/__test__/get_anomaly_aggs.test.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/__test__/get_anomaly_aggs.test.ts new file mode 100644 index 0000000000000..7ded075a7ae65 --- /dev/null +++ b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/__test__/get_anomaly_aggs.test.ts @@ -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); +}); diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_anomaly_aggs.js b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_anomaly_aggs.js index 86fb84fd88871..cae78df06f725 100644 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_anomaly_aggs.js +++ b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_anomaly_aggs.js @@ -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; } } diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_avg_response_time_anomalies.js b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_avg_response_time_anomalies.js index c6d411eb91884..3e950d919b6cc 100644 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_avg_response_time_anomalies.js +++ b/x-pack/plugins/apm/server/lib/transactions/charts/get_avg_response_time_anomalies/get_avg_response_time_anomalies.js @@ -34,7 +34,7 @@ export async function getAvgResponseTimeAnomalies({ if (!aggs) { return { - message: 'ml index does not exist' + message: 'Error reading machine learning index' }; }