Skip to content

Commit

Permalink
refactor error utilities to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Jul 1, 2020
1 parent 7861f0b commit b41e08f
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import { EuiButton, EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiSpacer } from
import { useForm, Form, FormConfig } from '../../../shared_imports';
import { Pipeline } from '../../../../common/types';

import { PipelineRequestFlyout } from './pipeline_request_flyout';
import { PipelineTestFlyout } from './pipeline_test_flyout';
import { PipelineFormFields } from './pipeline_form_fields';
import { PipelineFormError } from './pipeline_form_error';
import { pipelineFormSchema } from './schema';
import {
OnUpdateHandlerArg,
OnUpdateHandler,
SerializeResult,
} from '../pipeline_processors_editor';

import { PipelineRequestFlyout } from './pipeline_request_flyout';
import { PipelineTestFlyout } from './pipeline_test_flyout';
import { PipelineFormFields } from './pipeline_form_fields';
import { PipelineFormError } from './pipeline_form_error';
import { pipelineFormSchema } from './schema';

export interface PipelineFormProps {
onSave: (pipeline: Pipeline) => void;
onCancel: () => void;
Expand Down

This file was deleted.

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

import { toKnownError } from './pipeline_form_error';
import { nestedProcessorsErrorFixture } from '../../../../__jest__/client_integration/fixtures';
import { toKnownError } from './error_utils';
import { nestedProcessorsErrorFixture } from '../../../../../__jest__/client_integration/fixtures';

describe('toKnownError', () => {
test('undefined, null, numbers, arrays and bad objects', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 * as t from 'io-ts';
import { flow } from 'fp-ts/lib/function';
import { isRight } from 'fp-ts/lib/Either';

import { i18nTexts } from './i18n_texts';

export interface PipelineError {
reason: string;
processorType?: string;
}
interface PipelineErrors {
errors: PipelineError[];
}

interface ErrorNode {
reason: string;
processor_type?: string;
suppressed?: ErrorNode[];
}

// This is a runtime type (RT) for an error node which is a recursive type
const errorNodeRT = t.recursion<ErrorNode>('ErrorNode', (ErrorNode) =>
t.intersection([
t.interface({
reason: t.string,
}),
t.partial({
processor_type: t.string,
suppressed: t.array(ErrorNode),
}),
])
);

// This is a runtime type for the attributes object we expect to receive from the server
// for processor errors
const errorAttributesObjectRT = t.interface({
attributes: t.interface({
error: t.interface({
root_cause: t.array(errorNodeRT),
}),
}),
});

const isProcessorsError = flow(errorAttributesObjectRT.decode, isRight);

type ErrorAttributesObject = t.TypeOf<typeof errorAttributesObjectRT>;

const flattenErrorsTree = (node: ErrorNode): PipelineError[] => {
const result: PipelineError[] = [];
const recurse = (_node: ErrorNode) => {
result.push({ reason: _node.reason, processorType: _node.processor_type });
if (_node.suppressed && Array.isArray(_node.suppressed)) {
_node.suppressed.forEach(recurse);
}
};
recurse(node);
return result;
};

export const toKnownError = (error: unknown): PipelineErrors => {
if (typeof error === 'object' && error != null && isProcessorsError(error)) {
const errorAttributes = error as ErrorAttributesObject;
const rootCause = errorAttributes.attributes.error.root_cause[0];
return { errors: flattenErrorsTree(rootCause) };
}

if (typeof error === 'string') {
return { errors: [{ reason: error }] };
}

if (
error instanceof Error ||
(typeof error === 'object' && error != null && (error as any).message)
) {
return { errors: [{ reason: (error as any).message }] };
}

return { errors: [{ reason: i18nTexts.errors.unknownError }] };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { i18n } from '@kbn/i18n';

export const i18nTexts = {
title: i18n.translate('xpack.ingestPipelines.form.savePipelineError', {
defaultMessage: 'Unable to create pipeline',
}),
errors: {
processor: (processorType: string) =>
i18n.translate('xpack.ingestPipelines.form.savePipelineError.processorLabel', {
defaultMessage: '{type} processor',
values: { type: processorType },
}),
showErrors: (hiddenErrorsCount: number) =>
i18n.translate('xpack.ingestPipelines.form.savePipelineError.showAllButton', {
defaultMessage:
'Show {hiddenErrorsCount, plural, one {# more error} other {# more errors}}',
values: {
hiddenErrorsCount,
},
}),
hideErrors: (hiddenErrorsCount: number) =>
i18n.translate('xpack.ingestPipelines.form.savePip10mbelineError.showFewerButton', {
defaultMessage: 'Hide {hiddenErrorsCount, plural, one {# error} other {# errors}}',
values: {
hiddenErrorsCount,
},
}),
unknownError: i18n.translate('xpack.ingestPipelines.form.unknownError', {
defaultMessage: 'An unknown error occurred.',
}),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { PipelineFormError } from './pipeline_form_error';
Loading

0 comments on commit b41e08f

Please sign in to comment.