Skip to content

Commit

Permalink
updated to use i18n in one throw
Browse files Browse the repository at this point in the history
  • Loading branch information
yctercero committed Feb 24, 2021
1 parent e679318 commit a4fd2f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 15 additions & 0 deletions x-pack/plugins/lists/public/exceptions/hooks/translations.ts
Original file line number Diff line number Diff line change
@@ -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.',
}
);
9 changes: 8 additions & 1 deletion x-pack/plugins/lists/public/exceptions/hooks/use_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,14 +50,20 @@ export const useApi = (http: HttpStart): ExceptionsApi => {
}): Promise<ExceptionListItemSchema> {
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,
listItem: sanitizedItem,
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({
Expand Down

0 comments on commit a4fd2f3

Please sign in to comment.