diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/index.ts b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/index.ts similarity index 80% rename from x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/index.ts rename to x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/index.ts index 441648a8701e0..a9a3c61472d8c 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/index.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/index.ts @@ -4,4 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -export { isEsErrorFactory } from './is_es_error_factory'; +export { isEsError } from './is_es_error'; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/is_es_error.ts b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/is_es_error.ts new file mode 100644 index 0000000000000..4137293cf39c0 --- /dev/null +++ b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error/is_es_error.ts @@ -0,0 +1,13 @@ +/* + * 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 * as legacyElasticsearch from 'elasticsearch'; + +const esErrorsParent = legacyElasticsearch.errors._Abstract; + +export function isEsError(err: Error) { + return err instanceof esErrorsParent; +} diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/__tests__/is_es_error_factory.js b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/__tests__/is_es_error_factory.js deleted file mode 100644 index 76fdf7b36c3d0..0000000000000 --- a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/__tests__/is_es_error_factory.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import { isEsErrorFactory } from '../is_es_error_factory'; -import { set } from 'lodash'; - -class MockAbstractEsError {} - -describe('is_es_error_factory', () => { - - let mockServer; - let isEsError; - - beforeEach(() => { - const mockEsErrors = { - _Abstract: MockAbstractEsError - }; - mockServer = {}; - set(mockServer, 'plugins.elasticsearch.getCluster', () => ({ errors: mockEsErrors })); - - isEsError = isEsErrorFactory(mockServer); - }); - - describe('#isEsErrorFactory', () => { - - it('should return a function', () => { - expect(isEsError).to.be.a(Function); - }); - - describe('returned function', () => { - - it('should return true if passed-in err is a known esError', () => { - const knownEsError = new MockAbstractEsError(); - expect(isEsError(knownEsError)).to.be(true); - }); - - it('should return false if passed-in err is not a known esError', () => { - const unknownEsError = {}; - expect(isEsError(unknownEsError)).to.be(false); - - }); - }); - }); -}); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/is_es_error_factory.ts b/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/is_es_error_factory.ts deleted file mode 100644 index c79eb5866803f..0000000000000 --- a/x-pack/legacy/plugins/watcher/server/np_ready/lib/is_es_error_factory/is_es_error_factory.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 { memoize } from 'lodash'; -import { ServerShim } from '../../types'; - -const esErrorsFactory = memoize((server: ServerShim) => { - return (server.plugins.elasticsearch.getCluster('admin') as any).errors; -}); - -export function isEsErrorFactory(server: ServerShim) { - const esErrors = esErrorsFactory(server); - return function isEsError(err: Error) { - return err instanceof esErrors._Abstract; - }; -} diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/indices/register_get_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/indices/register_get_route.ts index 31bc4c785e300..fecc5e016de5c 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/indices/register_get_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/indices/register_get_route.ts @@ -7,7 +7,7 @@ import { RequestHandler } from 'src/core/server'; import { reduce, size } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; @@ -61,7 +61,6 @@ function getIndices(callWithRequest: any, pattern: string, limit = 10) { } export function registerGetRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const { pattern } = request.body; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_list_fields_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_list_fields_route.ts index 3ec218f25ffa6..75622ce12adb3 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_list_fields_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_list_fields_route.ts @@ -6,7 +6,7 @@ import { RequestHandler } from 'src/core/server'; import { callWithRequestFactory } from '../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../lib/is_es_error_factory'; +import { isEsError } from '../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; // @ts-ignore import { Fields } from '../../models/fields'; @@ -25,8 +25,6 @@ function fetchFields(callWithRequest: any, indexes: string[]) { } export function registerListFieldsRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); - const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const { indexes } = request.body; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_load_history_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_load_history_route.ts index 869293334478e..d62e4f48c5629 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_load_history_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_load_history_route.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { get } from 'lodash'; import { RequestHandler } from 'src/core/server'; import { callWithRequestFactory } from '../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../lib/is_es_error_factory'; +import { isEsError } from '../../lib/is_es_error'; import { INDEX_NAMES } from '../../../../common/constants'; import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../types'; @@ -29,7 +29,6 @@ function fetchHistoryItem(callWithRequest: any, watchHistoryItemId: string) { } export function registerLoadHistoryRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const id = request.params.id; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/settings/register_load_route.ts index 5c8818b43f9a4..710d079d810da 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/settings/register_load_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/settings/register_load_route.ts @@ -5,7 +5,7 @@ */ import { IClusterClient, RequestHandler } from 'src/core/server'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; // @ts-ignore import { Settings } from '../../../models/settings'; @@ -19,7 +19,6 @@ function fetchClusterSettings(client: IClusterClient) { } export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { try { const settings = await fetchClusterSettings(deps.elasticsearch); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/action/register_acknowledge_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/action/register_acknowledge_route.ts index 0be94dda47cb3..d0cc0a27e87ff 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/action/register_acknowledge_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/action/register_acknowledge_route.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { get } from 'lodash'; import { RequestHandler } from 'src/core/server'; import { callWithRequestFactory } from '../../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../../lib/is_es_error_factory'; +import { isEsError } from '../../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../../lib/license_pre_routing_factory'; // @ts-ignore import { WatchStatus } from '../../../../models/watch_status'; @@ -22,7 +22,6 @@ function acknowledgeAction(callWithRequest: any, watchId: string, actionId: stri } export function registerAcknowledgeRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const { watchId, actionId } = request.params; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_activate_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_activate_route.ts index 3f7d132b7d352..28c482124aaee 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_activate_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_activate_route.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; // @ts-ignore @@ -21,7 +21,6 @@ function activateWatch(callWithRequest: any, watchId: string) { } export function registerActivateRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_deactivate_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_deactivate_route.ts index 9108190489fe5..ac87066379a20 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_deactivate_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_deactivate_route.ts @@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; // @ts-ignore @@ -20,7 +20,6 @@ function deactivateWatch(callWithRequest: any, watchId: string) { } export function registerDeactivateRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_delete_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_delete_route.ts index 5e974d48dff0e..3402cc283dba0 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_delete_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_delete_route.ts @@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; @@ -18,7 +18,6 @@ function deleteWatch(callWithRequest: any, watchId: string) { } export function registerDeleteRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_execute_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_execute_route.ts index 06da7ad8341d3..f3bce228653fe 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_execute_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_execute_route.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; @@ -29,7 +29,6 @@ function executeWatch(callWithRequest: any, executeDetails: any, watchJson: any) } export function registerExecuteRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const executeDetails = ExecuteDetails.fromDownstreamJson(request.body.executeDetails); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_history_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_history_route.ts index 49d83485c9ec9..e236d7dd642a3 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_history_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_history_route.ts @@ -10,7 +10,7 @@ import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; import { fetchAllFromScroll } from '../../../lib/fetch_all_from_scroll'; import { INDEX_NAMES, ES_SCROLL_SETTINGS } from '../../../../../common/constants'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; // @ts-ignore @@ -43,7 +43,6 @@ function fetchHistoryItems(callWithRequest: any, watchId: any, startTime: any) { } export function registerHistoryRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const { watchId } = request.params; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_load_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_load_route.ts index 17f59300da73f..7311ad08f73a6 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_load_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_load_route.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; // @ts-ignore import { Watch } from '../../../models/watch'; @@ -21,7 +21,6 @@ function fetchWatch(callWithRequest: any, watchId: string) { } export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_save_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_save_route.ts index f16bd233040bd..5d22392d49ed8 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_save_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_save_route.ts @@ -13,7 +13,7 @@ import { serializeThresholdWatch, } from '../../../../../common/lib/serialization'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; @@ -31,7 +31,6 @@ function saveWatch(callWithRequest: any, id: string, body: any) { } export function registerSaveRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const { id } = request.params; diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_visualize_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_visualize_route.ts index d7f4158a4eebc..d07a264b0b2b1 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_visualize_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_visualize_route.ts @@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandler } from 'src/core/server'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; @@ -29,7 +29,6 @@ function fetchVisualizeData(callWithRequest: any, index: any, body: any) { } export function registerVisualizeRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request); const watch = Watch.fromDownstreamJson(request.body.watch); diff --git a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watches/register_list_route.ts b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watches/register_list_route.ts index db90b3f5b26ff..b94c29e0f9892 100644 --- a/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watches/register_list_route.ts +++ b/x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watches/register_list_route.ts @@ -9,7 +9,7 @@ import { get } from 'lodash'; import { callWithRequestFactory } from '../../../lib/call_with_request_factory'; import { fetchAllFromScroll } from '../../../lib/fetch_all_from_scroll'; import { INDEX_NAMES, ES_SCROLL_SETTINGS } from '../../../../../common/constants'; -import { isEsErrorFactory } from '../../../lib/is_es_error_factory'; +import { isEsError } from '../../../lib/is_es_error'; import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory'; import { RouteDependencies, ServerShim } from '../../../types'; // @ts-ignore @@ -31,7 +31,6 @@ function fetchWatches(callWithRequest: any) { } export function registerListRoute(deps: RouteDependencies, legacy: ServerShim) { - const isEsError = isEsErrorFactory(legacy); const handler: RequestHandler = async (ctx, request, response) => { const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);