From a4fd2f35aa96ef706148fb4998c053a0293a806c Mon Sep 17 00:00:00 2001 From: Yara Tercero Date: Tue, 23 Feb 2021 18:06:10 -0800 Subject: [PATCH] updated to use i18n in one throw --- .../lists/public/exceptions/hooks/translations.ts | 15 +++++++++++++++ .../lists/public/exceptions/hooks/use_api.ts | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugins/lists/public/exceptions/hooks/translations.ts diff --git a/x-pack/plugins/lists/public/exceptions/hooks/translations.ts b/x-pack/plugins/lists/public/exceptions/hooks/translations.ts new file mode 100644 index 00000000000000..41c64216159626 --- /dev/null +++ b/x-pack/plugins/lists/public/exceptions/hooks/translations.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { i18n } from '@kbn/i18n'; + +export const ADD_EXCEPTION_ITEM_TYPE_ERROR = i18n.translate( + 'xpack.lists.exceptions.hooks.addExceptionItemTypeError', + { + defaultMessage: 'Unable to create exception item. Item malformed.', + } +); diff --git a/x-pack/plugins/lists/public/exceptions/hooks/use_api.ts b/x-pack/plugins/lists/public/exceptions/hooks/use_api.ts index 28cfcb167ca8cf..878b5e3836afe8 100644 --- a/x-pack/plugins/lists/public/exceptions/hooks/use_api.ts +++ b/x-pack/plugins/lists/public/exceptions/hooks/use_api.ts @@ -20,6 +20,7 @@ import { ApiCallFindListsItemsMemoProps, ApiCallMemoProps, ApiListExportProps } import { getIdsAndNamespaces } from '../utils'; import { transformInput, transformOutput } from '../transforms'; +import * as i18n from './translations'; export interface ExceptionsApi { addExceptionListItem: (arg: { listItem: CreateExceptionListItemSchema; @@ -49,6 +50,12 @@ export const useApi = (http: HttpStart): ExceptionsApi => { }): Promise { const abortCtrl = new AbortController(); const sanitizedItem = transformOutput(listItem); + + // This was added to satisfy Typescript, which I don't love, + // but felt better than doing an `as` cast. Wasn't able to + // figure out how to do a generic to specify that input is of + // type A or B, and if it is A returns A, if B returns B. Can + // delete this if/else statement if figured out if (createExceptionListItemSchema.is(sanitizedItem)) { return Api.addExceptionListItem({ http, @@ -56,7 +63,7 @@ export const useApi = (http: HttpStart): ExceptionsApi => { signal: abortCtrl.signal, }); } else { - throw new Error('Unable to create exception item. Item malformed.'); + throw new Error(i18n.ADD_EXCEPTION_ITEM_TYPE_ERROR); } }, async deleteExceptionItem({