Skip to content

Commit

Permalink
[ILM] Add esErrorHandler for the new es js client (#80302)
Browse files Browse the repository at this point in the history
* [ILM] Add esErrorHandler for the new es js client

* [ILM] Fix function call params

* Rename function

* Rename function

* Rename function

* [ILM] Change import of handleEsError to lib dependency

* [ILM] Add comments about legacy and new es js client

* [ILM] Add type casting for ResponseError

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
yuliacech and kibanamachine authored Oct 16, 2020
1 parent 8e51f0f commit 322094a
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { ApiError } from '@elastic/elasticsearch';
import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { IKibanaResponse, KibanaResponseFactory } from 'kibana/server';

interface EsErrorHandlerParams {
error: ApiError;
response: KibanaResponseFactory;
}

/*
* For errors returned by the new elasticsearch js client.
*/
export const handleEsError = ({ error, response }: EsErrorHandlerParams): IKibanaResponse => {
// error.name is slightly better in terms of performance, since all errors now have name property
if (error.name === 'ResponseError') {
const { statusCode, body } = error as ResponseError;
return response.customError({
statusCode,
body: { message: body.error?.reason },
});
}
// Case: default
return response.internalError({ body: error });
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { isEsError } from './is_es_error';
export { handleEsError } from './handle_es_error';
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface RequestError extends Error {
statusCode?: number;
}

/*
* @deprecated
* Only works with legacy elasticsearch js client errors and will be removed after 7.x last
*/
export function isEsError(err: RequestError) {
const isInstanceOfEsError = err instanceof esErrorsParent;
const hasStatusCode = Boolean(err.statusCode);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/es_ui_shared/server/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export { isEsError } from '../../__packages_do_not_import__/errors';
export { isEsError, handleEsError } from '../../__packages_do_not_import__/errors';
2 changes: 1 addition & 1 deletion src/plugins/es_ui_shared/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

export { isEsError } from './errors';
export { isEsError, handleEsError } from './errors';

/** dummy plugin*/
export function plugin() {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/index_lifecycle_management/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PluginInitializerContext,
LegacyAPICaller,
} from 'src/core/server';
import { handleEsError } from './shared_imports';

import { Index as IndexWithoutIlm } from '../../index_management/common/types';
import { PLUGIN } from '../common/constants';
Expand Down Expand Up @@ -99,6 +100,9 @@ export class IndexLifecycleManagementServerPlugin implements Plugin<void, void,
router,
config,
license: this.license,
lib: {
handleEsError,
},
});

if (config.ui.enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ const bodySchema = schema.object({
alias: schema.maybe(schema.string()),
});

export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
export function registerAddPolicyRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.post(
{ path: addBasePath('/index/add'), validate: { body: bodySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -47,15 +51,8 @@ export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
alias
);
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const bodySchema = schema.object({
indexNames: schema.arrayOf(schema.string()),
});

export function registerRemoveRoute({ router, license }: RouteDependencies) {
export function registerRemoveRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.post(
{ path: addBasePath('/index/remove'), validate: { body: bodySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -36,15 +40,8 @@ export function registerRemoveRoute({ router, license }: RouteDependencies) {
try {
await removeLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames);
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const bodySchema = schema.object({
indexNames: schema.arrayOf(schema.string()),
});

export function registerRetryRoute({ router, license }: RouteDependencies) {
export function registerRetryRoute({ router, license, lib: { handleEsError } }: RouteDependencies) {
router.post(
{ path: addBasePath('/index/retry'), validate: { body: bodySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -37,15 +37,8 @@ export function registerRetryRoute({ router, license }: RouteDependencies) {
try {
await retryLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames);
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const paramsSchema = schema.object({
nodeAttrs: schema.string(),
});

export function registerDetailsRoute({ router, license }: RouteDependencies) {
export function registerDetailsRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.get(
{ path: addBasePath('/nodes/{nodeAttrs}/details'), validate: { params: paramsSchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -40,15 +44,8 @@ export function registerDetailsRoute({ router, license }: RouteDependencies) {
const statsResponse = await context.core.elasticsearch.client.asCurrentUser.nodes.stats();
const okResponse = { body: findMatchingNodes(statsResponse.body, nodeAttrs) };
return response.ok(okResponse);
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export function convertSettingsIntoLists(
);
}

export function registerListRoute({ router, config, license }: RouteDependencies) {
export function registerListRoute({
router,
config,
license,
lib: { handleEsError },
}: RouteDependencies) {
const { filteredNodeAttributes } = config;

const NODE_ATTRS_KEYS_TO_IGNORE: string[] = [
Expand Down Expand Up @@ -95,15 +100,8 @@ export function registerListRoute({ router, config, license }: RouteDependencies
disallowedNodeAttributes
);
return response.ok({ body });
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ const bodySchema = schema.object({
}),
});

export function registerCreateRoute({ router, license }: RouteDependencies) {
export function registerCreateRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.post(
{ path: addBasePath('/policies'), validate: { body: bodySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -144,15 +148,8 @@ export function registerCreateRoute({ router, license }: RouteDependencies) {
try {
await createPolicy(context.core.elasticsearch.client.asCurrentUser, name, phases);
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const paramsSchema = schema.object({
policyNames: schema.string(),
});

export function registerDeleteRoute({ router, license }: RouteDependencies) {
export function registerDeleteRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.delete(
{ path: addBasePath('/policies/{policyNames}'), validate: { params: paramsSchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -33,15 +37,8 @@ export function registerDeleteRoute({ router, license }: RouteDependencies) {
try {
await deletePolicies(context.core.elasticsearch.client.asCurrentUser, policyNames);
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const querySchema = schema.object({
withIndices: schema.boolean({ defaultValue: false }),
});

export function registerFetchRoute({ router, license }: RouteDependencies) {
export function registerFetchRoute({ router, license, lib: { handleEsError } }: RouteDependencies) {
router.get(
{ path: addBasePath('/policies'), validate: { query: querySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -75,15 +75,8 @@ export function registerFetchRoute({ router, license }: RouteDependencies) {
await addLinkedIndices(asCurrentUser, policiesMap);
}
return response.ok({ body: formatPolicies(policiesMap) });
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function fetchSnapshotPolicies(client: ElasticsearchClient): Promise<any>
return response.body;
}

export function registerFetchRoute({ router, license }: RouteDependencies) {
export function registerFetchRoute({ router, license, lib: { handleEsError } }: RouteDependencies) {
router.get(
{ path: addBasePath('/snapshot_policies'), validate: false },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -23,15 +23,8 @@ export function registerFetchRoute({ router, license }: RouteDependencies) {
context.core.elasticsearch.client.asCurrentUser
);
return response.ok({ body: Object.keys(policiesByName) });
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ const querySchema = schema.object({
legacy: schema.maybe(schema.oneOf([schema.literal('true'), schema.literal('false')])),
});

export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
export function registerAddPolicyRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.post(
{ path: addBasePath('/template'), validate: { body: bodySchema, query: querySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -118,15 +122,8 @@ export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
});
}
return response.ok();
} catch (e) {
if (e.name === 'ResponseError') {
return response.customError({
statusCode: e.statusCode,
body: { message: e.body.error?.reason },
});
}
// Case: default
return response.internalError({ body: e });
} catch (error) {
return handleEsError({ error, response });
}
})
);
Expand Down
Loading

0 comments on commit 322094a

Please sign in to comment.