Skip to content

Commit

Permalink
[ML] Fixing reporting of http request errors (elastic#61811)
Browse files Browse the repository at this point in the history
* [ML] Fixing reporting of http request errors

* fixing types

* updating translations

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
jgowdyelastic and elasticmachine committed Mar 31, 2020
1 parent 538c4a5 commit 1bd4203
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 44 deletions.
19 changes: 19 additions & 0 deletions x-pack/plugins/ml/common/util/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { isErrorResponse } from '../types/errors';

export function getErrorMessage(error: any) {
if (isErrorResponse(error)) {
return `${error.body.error}: ${error.body.message}`;
}

if (typeof error === 'object' && typeof error.message === 'string') {
return error.message;
}

return JSON.stringify(error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Subscription } from 'rxjs';
import { cloneDeep } from 'lodash';
import { ml } from '../../services/ml_api_service';
import { Dictionary } from '../../../../common/types/common';
import { getErrorMessage } from '../pages/analytics_management/hooks/use_create_analytics_form';
import { getErrorMessage } from '../../../../common/util/errors';
import { SavedSearchQuery } from '../../contexts/ml';
import { SortDirection } from '../../components/ml_in_memory_table';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export {
useCreateAnalyticsForm,
CreateAnalyticsFormProps,
getErrorMessage,
} from './use_create_analytics_form';
export { useCreateAnalyticsForm, CreateAnalyticsFormProps } from './use_create_analytics_form';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { mountHook } from 'test_utils/enzyme_helpers';
import { MlContext } from '../../../../../contexts/ml';
import { kibanaContextValueMock } from '../../../../../contexts/ml/__mocks__/kibana_context_value';

import { getErrorMessage, useCreateAnalyticsForm } from './use_create_analytics_form';
import { useCreateAnalyticsForm } from './use_create_analytics_form';
import { getErrorMessage } from '../../../../../../../common/util/errors';

const getMountedHook = () =>
mountHook(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useReducer } from 'react';
import { i18n } from '@kbn/i18n';

import { SimpleSavedObject } from 'kibana/public';
import { isErrorResponse } from '../../../../../../../common/types/errors';
import { getErrorMessage } from '../../../../../../../common/util/errors';
import { DeepReadonly } from '../../../../../../../common/types/common';
import { ml } from '../../../../../services/ml_api_service';
import { useMlContext } from '../../../../../contexts/ml';
Expand Down Expand Up @@ -41,18 +41,6 @@ export interface CreateAnalyticsFormProps {
state: State;
}

export function getErrorMessage(error: any) {
if (isErrorResponse(error)) {
return `${error.body.error}: ${error.body.message}`;
}

if (typeof error === 'object' && typeof error.message === 'string') {
return error.message;
}

return JSON.stringify(error);
}

export const useCreateAnalyticsForm = (): CreateAnalyticsFormProps => {
const mlContext = useMlContext();
const [state, dispatch] = useReducer(reducer, getInitialState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { JobRunner } from '../../../../../common/job_runner';
import { useMlKibana } from '../../../../../../../contexts/kibana';
import { getErrorMessage } from '../../../../../../../../../common/util/errors';

// @ts-ignore
import { CreateWatchFlyout } from '../../../../../../jobs_list/components/create_watch_flyout/index';
Expand Down Expand Up @@ -69,7 +70,7 @@ export const PostSaveOptions: FC<Props> = ({ jobRunner }) => {
defaultMessage: `Error starting job`,
}
),
text: error.message,
text: getErrorMessage(error),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { mlJobService } from '../../../../../services/job_service';
import { JsonEditorFlyout, EDITOR_MODE } from '../common/json_editor_flyout';
import { DatafeedPreviewFlyout } from '../common/datafeed_preview_flyout';
import { JOB_TYPE } from '../../../../../../../common/constants/new_job';
import { getErrorMessage } from '../../../../../../../common/util/errors';
import { isSingleMetricJobCreator, isAdvancedJobCreator } from '../../../common/job_creator';
import { JobDetails } from './components/job_details';
import { DatafeedDetails } from './components/datafeed_details';
Expand Down Expand Up @@ -75,7 +76,7 @@ export const SummaryStep: FC<StepProps> = ({ setCurrentStep, isCurrentStep }) =>
title: i18n.translate('xpack.ml.newJob.wizard.summaryStep.createJobError', {
defaultMessage: `Job creation error`,
}),
text: error.message,
text: getErrorMessage(error),
});
setCreatingJob(false);
}
Expand All @@ -94,7 +95,7 @@ export const SummaryStep: FC<StepProps> = ({ setCurrentStep, isCurrentStep }) =>
title: i18n.translate('xpack.ml.newJob.wizard.summaryStep.createJobError', {
defaultMessage: `Job creation error`,
}),
text: error.message,
text: getErrorMessage(error),
});
setCreatingJob(false);
}
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/ml/public/application/services/job_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ class JobService {

function error(err) {
console.log('jobService error getting list of jobs:', err);
msgs.error(
msgs.notify.error(
i18n.translate('xpack.ml.jobService.jobsListCouldNotBeRetrievedErrorMessage', {
defaultMessage: 'Jobs list could not be retrieved',
})
);
msgs.error('', err);
msgs.notify.error('', err);
reject({ jobs, err });
}
});
Expand Down Expand Up @@ -256,12 +256,12 @@ class JobService {

function error(err) {
console.log('JobService error getting list of jobs:', err);
msgs.error(
msgs.notify.error(
i18n.translate('xpack.ml.jobService.jobsListCouldNotBeRetrievedErrorMessage', {
defaultMessage: 'Jobs list could not be retrieved',
})
);
msgs.error('', err);
msgs.notify.error('', err);
reject({ jobs, err });
}
});
Expand Down Expand Up @@ -302,12 +302,12 @@ class JobService {

function error(err) {
console.log('loadDatafeeds error getting list of datafeeds:', err);
msgs.error(
msgs.notify.error(
i18n.translate('xpack.ml.jobService.datafeedsListCouldNotBeRetrievedErrorMessage', {
defaultMessage: 'datafeeds list could not be retrieved',
})
);
msgs.error('', err);
msgs.notify.error('', err);
reject({ jobs, err });
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { getToastNotifications } from '../../../util/dependency_cache';
import { ml } from '../../../services/ml_api_service';
import { i18n } from '@kbn/i18n';
import { getErrorMessage } from '../../../../../common/util/errors';

export async function deleteCalendars(calendarsToDelete, callback) {
if (calendarsToDelete === undefined || calendarsToDelete.length === 0) {
Expand Down Expand Up @@ -36,17 +37,18 @@ export async function deleteCalendars(calendarsToDelete, callback) {
await ml.deleteCalendar({ calendarId });
} catch (error) {
console.log('Error deleting calendar:', error);
const errorMessage = i18n.translate(
'xpack.ml.calendarsList.deleteCalendars.deletingCalendarErrorMessage',
{
defaultMessage: 'An error occurred deleting calendar {calendarId}{errorMessage}',
values: {
calendarId: calendar.calendar_id,
errorMessage: error.message ? ` : ${error.message}` : '',
},
}
);
toastNotifications.addDanger(errorMessage);
toastNotifications.addDanger({
title: i18n.translate(
'xpack.ml.calendarsList.deleteCalendars.deletingCalendarErrorMessage',
{
defaultMessage: 'An error occurred deleting calendar {calendarId}',
values: {
calendarId: calendar.calendar_id,
},
}
),
text: getErrorMessage(error),
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import { KbnError } from '../../../../../../src/plugins/kibana_utils/public';

export class MLRequestFailure extends KbnError {
origError: any;
resp: any;
// takes an Error object and and optional response object
// if error is falsy (null) the response object will be used
// notify will show the full expandable stack trace of the response if a response object is used and no error is passed in.
constructor(error, resp) {
constructor(error: any, resp: any) {
error = error || {};
super(error.message || JSON.stringify(resp), MLRequestFailure);
super(error.message || JSON.stringify(resp));

this.origError = error;
this.resp = typeof resp === 'string' ? JSON.parse(resp) : resp;
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -7420,7 +7420,6 @@
"xpack.ml.calendarsEdit.newEventModal.startDateAriaLabel": "開始日",
"xpack.ml.calendarsEdit.newEventModal.toLabel": "終了:",
"xpack.ml.calendarsList.deleteCalendars.calendarsLabel": "{calendarsToDeleteCount} 件のカレンダー",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarErrorMessage": "カレンダー {calendarId} の削除中にエラーが発生しました: {errorMessage}",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarsNotificationMessage": "{messageId} を削除中",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarSuccessNotificationMessage": "{messageId} が選択されました",
"xpack.ml.calendarsList.deleteCalendarsModal.cancelButtonLabel": "キャンセル",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -7420,7 +7420,6 @@
"xpack.ml.calendarsEdit.newEventModal.startDateAriaLabel": "开始日期",
"xpack.ml.calendarsEdit.newEventModal.toLabel": "到:",
"xpack.ml.calendarsList.deleteCalendars.calendarsLabel": "{calendarsToDeleteCount} 个日历",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarErrorMessage": "删除日历 {calendarId} 时出错。{errorMessage}",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarsNotificationMessage": "正在删除 {messageId}",
"xpack.ml.calendarsList.deleteCalendars.deletingCalendarSuccessNotificationMessage": "已删除 {messageId}",
"xpack.ml.calendarsList.deleteCalendarsModal.cancelButtonLabel": "取消",
Expand Down

0 comments on commit 1bd4203

Please sign in to comment.