Skip to content

Commit

Permalink
Fix the error handling when config index is not found (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#173)

Signed-off-by: Annie Lee <leeyun@amazon.com>
  • Loading branch information
annie3431 authored and Annie Lee committed Feb 17, 2022
1 parent 4172e1b commit 24d368f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/services/DestinationsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

import _ from 'lodash';
import { isIndexNotFoundError } from './utils/helpers';

export default class DestinationsService {
constructor(esDriver) {
Expand Down Expand Up @@ -198,6 +199,11 @@ export default class DestinationsService {
},
});
} catch (err) {
if (isIndexNotFoundError(err)) {
return res.ok({
body: { ok: true, resp: { totalDestinations: 0, destinations: [] } },
});
}
return res.ok({
body: {
ok: false,
Expand Down
6 changes: 6 additions & 0 deletions server/services/MonitorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import _ from 'lodash';

import { INDEX } from '../../utils/constants';
import { isIndexNotFoundError } from './utils/helpers';

export default class MonitorService {
constructor(esDriver) {
Expand Down Expand Up @@ -350,6 +351,11 @@ export default class MonitorService {
});
} catch (err) {
console.error('Alerting - MonitorService - getMonitors', err);
if (isIndexNotFoundError(err)) {
return res.ok({
body: { ok: false, resp: { totalMonitors: 0, monitors: [] } },
});
}
return res.ok({
body: {
ok: false,
Expand Down
10 changes: 9 additions & 1 deletion server/services/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* permissions and limitations under the License.
*/

import { map, mapKeys, mapValues, isPlainObject, snakeCase, camelCase } from 'lodash';
import { get, map, mapKeys, mapValues, isPlainObject, snakeCase, camelCase } from 'lodash';

export function mapKeysDeep(obj, fn) {
if (Array.isArray(obj)) {
Expand All @@ -39,3 +39,11 @@ export function mapKeysDeep(obj, fn) {
export const toSnake = (value, key) => snakeCase(key);

export const toCamel = (value, key) => camelCase(key);

export const isIndexNotFoundError = (err) => {
return (
err.statusCode === 404 &&
get(err, 'body.error.reason', '') ===
'Configured indices are not found: [.opendistro-alerting-config]'
);
};

0 comments on commit 24d368f

Please sign in to comment.