Skip to content

Commit

Permalink
avoid reliance on Boom and instead relly on SO helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 14, 2020
1 parent 9353f75 commit 876dea5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

import { isAlertSavedObjectNotFoundError } from './is_alert_not_found_error';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import uuid from 'uuid';

describe('isAlertSavedObjectNotFoundError', () => {
test('identifies SavedObjects Not Found errors', () => {
const id = uuid.v4();
// ensure the error created by SO parses as a string with the format we expect
expect(
`${SavedObjectsErrorHelpers.createGenericNotFoundError('alert', id)}`.includes(`alert/${id}`)
).toBe(true);

const errorBySavedObjectsHelper = SavedObjectsErrorHelpers.createGenericNotFoundError(
'alert',
id
);

expect(isAlertSavedObjectNotFoundError(errorBySavedObjectsHelper, id)).toBe(true);
});

test('identifies generic errors', () => {
const id = uuid.v4();
expect(isAlertSavedObjectNotFoundError(new Error(`not found`), id)).toBe(false);
});
});
11 changes: 11 additions & 0 deletions x-pack/plugins/alerting/server/lib/is_alert_not_found_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';

export function isAlertSavedObjectNotFoundError(err: Error, alertId: string) {
return SavedObjectsErrorHelpers.isNotFoundError(err) && `${err}`.includes(alertId);
}
10 changes: 1 addition & 9 deletions x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { pick, mapValues, omit, without } from 'lodash';
import Boom from 'boom';
import { Logger, SavedObject } from '../../../../../src/core/server';
import { TaskRunnerContext } from './task_runner_factory';
import { ConcreteTaskInstance } from '../../../../plugins/task_manager/server';
Expand All @@ -27,6 +26,7 @@ import { taskInstanceToAlertTaskInstance } from './alert_task_instance';
import { AlertInstances } from '../alert_instance/alert_instance';
import { EVENT_LOG_ACTIONS } from '../plugin';
import { IEvent, IEventLogger } from '../../../event_log/server';
import { isAlertSavedObjectNotFoundError } from '../lib/is_alert_not_found_error';

const FALLBACK_RETRY_INTERVAL: IntervalSchedule = { interval: '5m' };

Expand Down Expand Up @@ -409,11 +409,3 @@ async function errorAsAlertTaskRunResult(
};
}
}

function isAlertSavedObjectNotFoundError(err: Error | Boom, alertId: string) {
return (
Boom.isBoom(err) &&
err?.output?.statusCode === 404 &&
err?.output?.payload?.message?.includes(alertId)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

import { isTaskSavedObjectNotFoundError } from './is_task_not_found_error';
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
import uuid from 'uuid';

describe('isTaskSavedObjectNotFoundError', () => {
test('identifies SavedObjects Not Found errors', () => {
const id = uuid.v4();
// ensure the error created by SO parses as a string with the format we expect
expect(
`${SavedObjectsErrorHelpers.createGenericNotFoundError('task', id)}`.includes(`task/${id}`)
).toBe(true);

const errorBySavedObjectsHelper = SavedObjectsErrorHelpers.createGenericNotFoundError(
'task',
id
);

expect(isTaskSavedObjectNotFoundError(errorBySavedObjectsHelper, id)).toBe(true);
});

test('identifies generic errors', () => {
const id = uuid.v4();
expect(isTaskSavedObjectNotFoundError(new Error(`not found`), id)).toBe(false);
});
});
11 changes: 11 additions & 0 deletions x-pack/plugins/task_manager/server/lib/is_task_not_found_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';

export function isTaskSavedObjectNotFoundError(err: Error, taskId: string) {
return SavedObjectsErrorHelpers.isNotFoundError(err) && `${err}`.includes(taskId);
}
9 changes: 1 addition & 8 deletions x-pack/plugins/task_manager/server/task_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { performance } from 'perf_hooks';
import Boom from 'boom';
import { Logger } from './types';
import { TaskRunner } from './task_runner';
import { isTaskSavedObjectNotFoundError } from './lib/is_task_not_found_error';

interface Opts {
maxWorkers: number;
Expand Down Expand Up @@ -169,11 +170,3 @@ function partitionListByCount<T>(list: T[], count: number): [T[], T[]] {
const listInCount = list.splice(0, count);
return [listInCount, list];
}

function isTaskSavedObjectNotFoundError(err: Error | Boom, taskId: string) {
return (
Boom.isBoom(err) &&
err?.output?.statusCode === 404 &&
err?.output?.payload?.message?.includes(taskId)
);
}

0 comments on commit 876dea5

Please sign in to comment.