-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Moves shared code to
@kbn/ml-error-utils
. (#155372)
- Moves code from `x-pack/plugins/ml/common/util/errors` that was shared via `x-pack/plugins/ml/public/shared.ts` to `@kbn/ml-error-utils`. - `data_visualizer` and `aiops` plugins now use that package instead of code duplication.
- Loading branch information
Showing
91 changed files
with
449 additions
and
611 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
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
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
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,3 @@ | ||
# @kbn/ml-error-utils | ||
|
||
Empty package generated by @kbn/generate |
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
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,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../../../..', | ||
roots: ['<rootDir>/x-pack/packages/ml/error_utils'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/ml-error-utils", | ||
"owner": "@elastic/ml-ui" | ||
} |
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,6 @@ | ||
{ | ||
"name": "@kbn/ml-error-utils", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0" | ||
} |
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
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
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
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,196 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type Boom from '@hapi/boom'; | ||
|
||
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
|
||
import type { IHttpFetchError } from '@kbn/core-http-browser'; | ||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
/** | ||
* Short hand type of estypes.ErrorCause. | ||
* @typedef {EsErrorRootCause} | ||
*/ | ||
export type EsErrorRootCause = estypes.ErrorCause; | ||
|
||
/** | ||
* Short hand type of estypes.ErrorResponseBase. | ||
* @typedef {EsErrorBody} | ||
*/ | ||
export type EsErrorBody = estypes.ErrorResponseBase; | ||
|
||
/** | ||
* ML Response error | ||
* @export | ||
* @interface MLResponseError | ||
* @typedef {MLResponseError} | ||
*/ | ||
export interface MLResponseError { | ||
/** | ||
* statusCode | ||
* @type {number} | ||
*/ | ||
statusCode: number; | ||
/** | ||
* error | ||
* @type {string} | ||
*/ | ||
error: string; | ||
/** | ||
* message | ||
* @type {string} | ||
*/ | ||
message: string; | ||
/** | ||
* Optional attributes | ||
* @type {?{ | ||
body: EsErrorBody; | ||
}} | ||
*/ | ||
attributes?: { | ||
body: EsErrorBody; | ||
}; | ||
} | ||
|
||
/** | ||
* Interface holding error message | ||
* @export | ||
* @interface ErrorMessage | ||
* @typedef {ErrorMessage} | ||
*/ | ||
export interface ErrorMessage { | ||
/** | ||
* message | ||
* @type {string} | ||
*/ | ||
message: string; | ||
} | ||
|
||
/** | ||
* To be used for client side errors related to search query bars. | ||
*/ | ||
export interface QueryErrorMessage extends ErrorMessage { | ||
/** | ||
* query | ||
* @type {string} | ||
*/ | ||
query: string; | ||
} | ||
|
||
/** | ||
* ML Error Object | ||
* @export | ||
* @interface MLErrorObject | ||
* @typedef {MLErrorObject} | ||
*/ | ||
export interface MLErrorObject { | ||
/** | ||
* Optional causedBy | ||
* @type {?string} | ||
*/ | ||
causedBy?: string; | ||
/** | ||
* message | ||
* @type {string} | ||
*/ | ||
message: string; | ||
/** | ||
* Optional statusCode | ||
* @type {?number} | ||
*/ | ||
statusCode?: number; | ||
/** | ||
* Optional fullError | ||
* @type {?EsErrorBody} | ||
*/ | ||
fullError?: EsErrorBody; | ||
} | ||
|
||
/** | ||
* MLHttpFetchErrorBase | ||
* @export | ||
* @interface MLHttpFetchErrorBase | ||
* @typedef {MLHttpFetchErrorBase} | ||
* @template T | ||
* @extends {IHttpFetchError<T>} | ||
*/ | ||
export interface MLHttpFetchErrorBase<T> extends IHttpFetchError<T> { | ||
/** | ||
* body | ||
* @type {T} | ||
*/ | ||
body: T; | ||
} | ||
|
||
/** | ||
* MLHttpFetchError | ||
* @export | ||
* @typedef {MLHttpFetchError} | ||
*/ | ||
export type MLHttpFetchError = MLHttpFetchErrorBase<MLResponseError>; | ||
|
||
/** | ||
* Union type of error types | ||
* @export | ||
* @typedef {ErrorType} | ||
*/ | ||
export type ErrorType = MLHttpFetchError | EsErrorBody | Boom.Boom | string | undefined; | ||
|
||
/** | ||
* Type guard to check if error is of type EsErrorBody | ||
* @export | ||
* @param {unknown} error | ||
* @returns {error is EsErrorBody} | ||
*/ | ||
export function isEsErrorBody(error: unknown): error is EsErrorBody { | ||
return isPopulatedObject(error, ['error']) && isPopulatedObject(error.error, ['reason']); | ||
} | ||
|
||
/** | ||
* Type guard to check if error is a string. | ||
* @export | ||
* @param {unknown} error | ||
* @returns {error is string} | ||
*/ | ||
export function isErrorString(error: unknown): error is string { | ||
return typeof error === 'string'; | ||
} | ||
|
||
/** | ||
* Type guard to check if error is of type ErrorMessage. | ||
* @export | ||
* @param {unknown} error | ||
* @returns {error is ErrorMessage} | ||
*/ | ||
export function isErrorMessage(error: unknown): error is ErrorMessage { | ||
return isPopulatedObject(error, ['message']) && typeof error.message === 'string'; | ||
} | ||
|
||
/** | ||
* Type guard to check if error is of type MLResponseError. | ||
* @export | ||
* @param {unknown} error | ||
* @returns {error is MLResponseError} | ||
*/ | ||
export function isMLResponseError(error: unknown): error is MLResponseError { | ||
return ( | ||
isPopulatedObject(error, ['body']) && | ||
isPopulatedObject(error.body, ['message']) && | ||
'message' in error.body | ||
); | ||
} | ||
|
||
/** | ||
* Type guard to check if error is of type Boom. | ||
* @export | ||
* @param {unknown} error | ||
* @returns {error is Boom.Boom} | ||
*/ | ||
export function isBoomError(error: unknown): error is Boom.Boom { | ||
return isPopulatedObject(error, ['isBoom']) && error.isBoom === true; | ||
} |
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,22 @@ | ||
{ | ||
"extends": "../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/ml-is-populated-object", | ||
"@kbn/core-http-browser", | ||
] | ||
} |
Oops, something went wrong.