Skip to content

Commit

Permalink
[Transform] Update tests and fix i18n duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jun 4, 2020
1 parent 1e6bd0c commit b32b884
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 281 deletions.
33 changes: 33 additions & 0 deletions x-pack/plugins/ml/common/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { CustomHttpResponseOptions, ResponseError } from 'kibana/server';
import { isErrorResponse } from '../types/errors';

export function getErrorMessage(error: any) {
Expand All @@ -17,3 +18,35 @@ export function getErrorMessage(error: any) {

return JSON.stringify(error);
}

export const extractErrorMessage = (
error: CustomHttpResponseOptions<ResponseError> | undefined | string
): string | undefined => {
if (typeof error === 'string') {
return error;
}

if (error?.body) {
if (typeof error.body === 'string') {
return error.body;
}
if (typeof error.body === 'object' && 'message' in error.body) {
if (typeof error.body.message === 'string') {
return error.body.message;
}
// @ts-ignore
if (typeof (error.body.message?.msg === 'string')) {
// @ts-ignore
return error.body.message?.msg;
}
}
if (typeof error.body === 'object' && 'msg' in error.body) {
// @ts-ignore
if (typeof error.body.msg === 'string') {
// @ts-ignore
return error.body.msg;
}
}
}
return undefined;
};
7 changes: 7 additions & 0 deletions x-pack/plugins/ml/public/application/util/error_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export const extractErrorMessage = (
return error.body.message?.msg;
}
}
if (typeof error.body === 'object' && 'msg' in error.body) {
// @ts-ignore
if (typeof error.body.msg === 'string') {
// @ts-ignore
return error.body.msg;
}
}
}
return undefined;
};
1 change: 1 addition & 0 deletions x-pack/plugins/ml/public/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from '../common/types/audit_message';

export * from '../common/util/anomaly_utils';
export * from '../common/util/errors';

export * from '../common/util/validators';

export * from './application/formatters/metric_change_description';
Expand Down
67 changes: 16 additions & 51 deletions x-pack/plugins/transform/public/app/hooks/use_delete_transform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,18 @@
*/

import React, { useCallback, useEffect, useState } from 'react';

import { i18n } from '@kbn/i18n';
import { CustomHttpResponseOptions, ResponseError } from 'kibana/server';
import { CoreSetup } from 'kibana/public';
import { toMountPoint } from '../../../../../../src/plugins/kibana_react/public';

import { TransformEndpointRequest, DeleteTransformEndpointResult } from '../../../common';

import { getErrorMessage } from '../../shared_imports';

import { getErrorMessage, extractErrorMessage } from '../../shared_imports';
import { useAppDependencies, useToastNotifications } from '../app_dependencies';
import { TransformListRow, refreshTransformList$, REFRESH_TRANSFORM_LIST_STATE } from '../common';
import { ToastNotificationText } from '../components';

import { useApi } from './use_api';
import { API_BASE_PATH } from '../../../common/constants';
import { IIndexPattern } from '../../../../../../src/plugins/data/common/index_patterns';

export const extractErrorMessage = (
error: CustomHttpResponseOptions<ResponseError> | undefined | string
): string | undefined => {
if (typeof error === 'string') {
return error;
}

if (error?.body) {
if (typeof error.body === 'string') {
return error.body;
}
if (typeof error.body === 'object' && 'message' in error.body) {
if (typeof error.body.message === 'string') {
return error.body.message;
}
// @ts-ignore
if (typeof (error.body.message?.msg === 'string')) {
// @ts-ignore
return error.body.message?.msg;
}
}
if (typeof error.body === 'object' && 'msg' in error.body) {
// @ts-ignore
if (typeof error.body.msg === 'string') {
// @ts-ignore
return error.body.msg;
}
}
}
return undefined;
};

export const canDeleteIndex = async (http: CoreSetup['http']) => {
const privilege = await http.get(`${API_BASE_PATH}privileges`);
if (!privilege) {
Expand Down Expand Up @@ -100,7 +62,7 @@ export const useDeleteIndexAndTargetIndex = (items: TransformListRow[]) => {

toastNotifications.addDanger(
i18n.translate(
'xpack.ml.dataframe.analyticsList.errorWithCheckingIfIndexPatternExistsNotificationErrorMessage',
'xpack.transform.deleteTransfrom.errorWithCheckingIfIndexPatternExistsNotificationErrorMessage',
{
defaultMessage:
'An error occurred checking if index pattern {indexPattern} exists: {error}',
Expand Down Expand Up @@ -201,7 +163,7 @@ export const useDeleteTransforms = () => {
if (status.destIndexDeleted?.success) {
toastNotifications.addSuccess(
i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteAnalyticsWithIndexSuccessMessage',
'xpack.transform.deleteTransfrom.deleteAnalyticsWithIndexSuccessMessage',
{
defaultMessage:
'Request to delete destination index {destinationIndex} acknowledged.',
Expand All @@ -213,7 +175,7 @@ export const useDeleteTransforms = () => {
if (status.destIndexPatternDeleted?.success) {
toastNotifications.addSuccess(
i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteAnalyticsWithIndexPatternSuccessMessage',
'xpack.transform.deleteTransfrom.deleteAnalyticsWithIndexPatternSuccessMessage',
{
defaultMessage:
'Request to delete index pattern {destinationIndex} acknowledged.',
Expand Down Expand Up @@ -248,7 +210,7 @@ export const useDeleteTransforms = () => {
const error = extractErrorMessage(status.destIndexDeleted.error);
toastNotifications.addDanger({
title: i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteAnalyticsWithIndexErrorMessage',
'xpack.transform.deleteTransfrom.deleteAnalyticsWithIndexErrorMessage',
{
defaultMessage: 'An error occurred deleting destination index {destinationIndex}',
values: { destinationIndex },
Expand All @@ -264,7 +226,7 @@ export const useDeleteTransforms = () => {
const error = extractErrorMessage(status.destIndexPatternDeleted.error);
toastNotifications.addDanger({
title: i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteAnalyticsWithIndexPatternErrorMessage',
'xpack.transform.deleteTransfrom.deleteAnalyticsWithIndexPatternErrorMessage',
{
defaultMessage: 'An error occurred deleting index pattern {destinationIndex}',
values: { destinationIndex },
Expand All @@ -281,7 +243,7 @@ export const useDeleteTransforms = () => {
if (isBulk) {
if (successCount.transformJobDeleted > 0) {
toastNotifications.addSuccess(
i18n.translate('xpack.transform.transformList.deleteTransformSuccessMessage', {
i18n.translate('xpack.transform.transformList.bulkDeleteTransformSuccessMessage', {
defaultMessage:
'Successfully deleted {count} transform {count, plural, one {job} other {jobs}}.',
values: { count: successCount.transformJobDeleted },
Expand All @@ -291,7 +253,7 @@ export const useDeleteTransforms = () => {

if (successCount.destIndexDeleted > 0) {
toastNotifications.addSuccess(
i18n.translate('xpack.transform.transformList.deleteTransformSuccessMessage', {
i18n.translate('xpack.transform.transformList.bulkDeleteDestIndexSuccessMessage', {
defaultMessage:
'Successfully deleted {count} destination {count, plural, one {index} other {indices}}.',
values: { count: successCount.destIndexDeleted },
Expand All @@ -300,11 +262,14 @@ export const useDeleteTransforms = () => {
}
if (successCount.destIndexPatternDeleted > 0) {
toastNotifications.addSuccess(
i18n.translate('xpack.transform.transformList.deleteTransformSuccessMessage', {
defaultMessage:
'Successfully deleted {count} destination index {count, plural, one {pattern} other {patterns}}.',
values: { count: successCount.destIndexPatternDeleted },
})
i18n.translate(
'xpack.transform.transformList.bulkDeleteDestIndexPatternSuccessMessage',
{
defaultMessage:
'Successfully deleted {count} destination index {count, plural, one {pattern} other {patterns}}.',
values: { count: successCount.destIndexPatternDeleted },
}
)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const DeleteAction: FC<DeleteActionProps> = ({ items, forceDisable }) =>
data-test-subj="mlAnalyticsJobDeleteIndexSwitch"
style={{ paddingBottom: 10 }}
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteDestinationIndexTitle',
'xpack.transform.actionDeleteTransform.bulkDeleteDestinationIndexTitle',
{
defaultMessage: 'Delete destination index',
}
Expand All @@ -116,7 +116,7 @@ export const DeleteAction: FC<DeleteActionProps> = ({ items, forceDisable }) =>
<EuiSwitch
data-test-subj="mlAnalyticsJobDeleteIndexPatternSwitch"
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteDestIndexPatternTitle',
'xpack.transform.actionDeleteTransform.bulkDeleteDestIndexPatternTitle',
{
defaultMessage: 'Delete index pattern',
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export const DeleteAction: FC<DeleteActionProps> = ({ items, forceDisable }) =>
data-test-subj="mlAnalyticsJobDeleteIndexSwitch"
style={{ paddingBottom: 10 }}
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteDestinationIndexTitle',
'xpack.transform.actionDeleteTransform.deleteDestinationIndexTitle',
{
defaultMessage: 'Delete destination index {destinationIndex}',
values: { destinationIndex: items[0] && items[0].config.dest.index },
Expand All @@ -161,7 +161,7 @@ export const DeleteAction: FC<DeleteActionProps> = ({ items, forceDisable }) =>
<EuiSwitch
data-test-subj="mlAnalyticsJobDeleteIndexPatternSwitch"
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteDestIndexPatternTitle',
'xpack.transform.actionDeleteTransform.deleteDestIndexPatternTitle',
{
defaultMessage: 'Delete index pattern {destinationIndex}',
values: { destinationIndex: items[0] && items[0].config.dest.index },
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/transform/public/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {

export {
getErrorMessage,
extractErrorMessage,
getDataGridSchemaFromKibanaFieldType,
getFieldsFromKibanaIndexPattern,
multiColumnSortFactory,
Expand Down
Loading

0 comments on commit b32b884

Please sign in to comment.