-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor error utilities to separate file
- Loading branch information
1 parent
7861f0b
commit b41e08f
Showing
7 changed files
with
237 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 0 additions & 200 deletions
200
...gins/ingest_pipelines/public/application/components/pipeline_form/pipeline_form_error.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
..._pipelines/public/application/components/pipeline_form/pipeline_form_error/error_utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] }; | ||
}; |
38 changes: 38 additions & 0 deletions
38
...t_pipelines/public/application/components/pipeline_form/pipeline_form_error/i18n_texts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
}), | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
...ingest_pipelines/public/application/components/pipeline_form/pipeline_form_error/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.