Skip to content

Commit

Permalink
[ML] Better error reporting when parsing JSON in file dataviz (elasti…
Browse files Browse the repository at this point in the history
…c#29123) (elastic#29160)

* [ML] Better error reporting when parsing JSON in file dataviz

* first step depends on json parsing
  • Loading branch information
jgowdyelastic authored Jan 24, 2019
1 parent 41f5fbe commit 8f06f7b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ function title(statuses) {
defaultMessage="Error reading file"
/>
);
case statuses.parseJSONStatus:
return (
<FormattedMessage
id="xpack.ml.fileDatavisualizer.importErrors.parsingJSONErrorMessage"
defaultMessage="Error parsing JSON"
/>
);
case statuses.indexCreatedStatus:
return (
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ImportProgress = injectI18n(function ({ statuses, intl }) {
const {
reading,
readStatus,
parseJSONStatus,
indexCreatedStatus,
ingestPipelineCreatedStatus,
indexPatternCreatedStatus,
Expand All @@ -37,7 +38,11 @@ export const ImportProgress = injectI18n(function ({ statuses, intl }) {

let completedStep = 0;

if (reading === true && readStatus === IMPORT_STATUS.INCOMPLETE) {
if (
reading === true &&
readStatus === IMPORT_STATUS.INCOMPLETE &&
parseJSONStatus === IMPORT_STATUS.INCOMPLETE
) {
completedStep = 0;
}
if (
Expand Down Expand Up @@ -183,8 +188,8 @@ export const ImportProgress = injectI18n(function ({ statuses, intl }) {
{
title: processFileTitle,
isSelected: true,
isComplete: (readStatus === IMPORT_STATUS.COMPLETE),
status: readStatus,
isComplete: (readStatus === IMPORT_STATUS.COMPLETE && parseJSONStatus === IMPORT_STATUS.COMPLETE),
status: (parseJSONStatus === IMPORT_STATUS.FAILED) ? parseJSONStatus : readStatus, // if JSON parsing failed, fail the first step
onClick: () => {},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DEFAULT_STATE = {
reading: false,
readProgress: 0,
readStatus: IMPORT_STATUS.INCOMPLETE,
parseJSONStatus: IMPORT_STATUS.INCOMPLETE,
indexCreatedStatus: IMPORT_STATUS.INCOMPLETE,
indexPatternCreatedStatus: IMPORT_STATUS.INCOMPLETE,
ingestPipelineCreatedStatus: IMPORT_STATUS.INCOMPLETE,
Expand Down Expand Up @@ -142,34 +143,62 @@ export class ImportView extends Component {
}, () => {
this.props.hideBottomBar();
setTimeout(async () => {
let success = false;
let success = true;
const createPipeline = (pipelineString !== '');

let indexCreationSettings = {};
let settings = {};
let mappings = {};
let pipeline = {};

try {
const settings = JSON.parse(indexSettingsString);
const mappings = JSON.parse(mappingsString);
indexCreationSettings = {
settings,
mappings,
};
if (createPipeline) {
indexCreationSettings.pipeline = JSON.parse(pipelineString);
}
settings = JSON.parse(indexSettingsString);
} catch (error) {
success = false;
const parseError = i18n.translate('xpack.ml.fileDatavisualizer.importView.parseSettingsError', {
defaultMessage: 'Error parsing settings:'
});
errors.push(`${parseError} ${error.message}`);
}

// if an @timestamp field has been added to the
// mappings, use this field as the time field.
// This relies on the field being populated by
// the ingest pipeline on ingest
if (mappings[DEFAULT_TIME_FIELD] !== undefined) {
timeFieldName = DEFAULT_TIME_FIELD;
this.setState({ timeFieldName });
}
try {
mappings = JSON.parse(mappingsString);
} catch (error) {
success = false;
const parseError = i18n.translate('xpack.ml.fileDatavisualizer.importView.parseMappingsError', {
defaultMessage: 'Error parsing mappings:'
});
errors.push(`${parseError} ${error.message}`);
}

const indexCreationSettings = {
settings,
mappings,
};

success = true;
try {
if (createPipeline) {
pipeline = JSON.parse(pipelineString);
indexCreationSettings.pipeline = pipeline;
}
} catch (error) {
success = false;
errors.push(error);
const parseError = i18n.translate('xpack.ml.fileDatavisualizer.importView.parsePipelineError', {
defaultMessage: 'Error parsing ingest pipeline:'
});
errors.push(`${parseError} ${error.message}`);
}

this.setState({
parseJSONStatus: success ? IMPORT_STATUS.COMPLETE : IMPORT_STATUS.FAILED,
});

// if an @timestamp field has been added to the
// mappings, use this field as the time field.
// This relies on the field being populated by
// the ingest pipeline on ingest
if (mappings[DEFAULT_TIME_FIELD] !== undefined) {
timeFieldName = DEFAULT_TIME_FIELD;
this.setState({ timeFieldName });
}

if (success) {
Expand Down Expand Up @@ -345,6 +374,7 @@ export class ImportView extends Component {
reading,
initialized,
readStatus,
parseJSONStatus,
indexCreatedStatus,
ingestPipelineCreatedStatus,
indexPatternCreatedStatus,
Expand All @@ -368,6 +398,7 @@ export class ImportView extends Component {
const statuses = {
reading,
readStatus,
parseJSONStatus,
indexCreatedStatus,
ingestPipelineCreatedStatus,
indexPatternCreatedStatus,
Expand Down

0 comments on commit 8f06f7b

Please sign in to comment.