Introduce the @kbn/es-errors package (#135613)

* create the package, move the files

* adapt imports

* fix more imports

* fix import from security plugin
This commit is contained in:
Pierre Gayvallet 2022-07-04 10:33:37 +02:00 committed by GitHub
parent f5c6c21bd5
commit aceea297c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 202 additions and 26 deletions

1
.github/CODEOWNERS vendored
View file

@ -290,6 +290,7 @@
/packages/kbn-logging/ @elastic/kibana-core
/packages/kbn-logging-mocks/ @elastic/kibana-core
/packages/kbn-http-tools/ @elastic/kibana-core
/packages/kbn-es-errors/ @elastic/kibana-core
/src/plugins/saved_objects_management/ @elastic/kibana-core
/src/plugins/advanced_settings/ @elastic/kibana-core
/x-pack/plugins/global_search_bar/ @elastic/kibana-core

View file

@ -195,6 +195,7 @@
"@kbn/crypto": "link:bazel-bin/packages/kbn-crypto",
"@kbn/datemath": "link:bazel-bin/packages/kbn-datemath",
"@kbn/doc-links": "link:bazel-bin/packages/kbn-doc-links",
"@kbn/es-errors": "link:bazel-bin/packages/kbn-es-errors",
"@kbn/es-query": "link:bazel-bin/packages/kbn-es-query",
"@kbn/eslint-plugin-imports": "link:bazel-bin/packages/kbn-eslint-plugin-imports",
"@kbn/field-types": "link:bazel-bin/packages/kbn-field-types",
@ -748,6 +749,7 @@
"@types/kbn__doc-links": "link:bazel-bin/packages/kbn-doc-links/npm_module_types",
"@types/kbn__docs-utils": "link:bazel-bin/packages/kbn-docs-utils/npm_module_types",
"@types/kbn__es-archiver": "link:bazel-bin/packages/kbn-es-archiver/npm_module_types",
"@types/kbn__es-errors": "link:bazel-bin/packages/kbn-es-errors/npm_module_types",
"@types/kbn__es-query": "link:bazel-bin/packages/kbn-es-query/npm_module_types",
"@types/kbn__eslint-plugin-imports": "link:bazel-bin/packages/kbn-eslint-plugin-imports/npm_module_types",
"@types/kbn__field-types": "link:bazel-bin/packages/kbn-field-types/npm_module_types",

View file

@ -93,6 +93,7 @@ filegroup(
"//packages/kbn-doc-links:build",
"//packages/kbn-docs-utils:build",
"//packages/kbn-es-archiver:build",
"//packages/kbn-es-errors:build",
"//packages/kbn-es-query:build",
"//packages/kbn-es:build",
"//packages/kbn-eslint-config:build",
@ -265,6 +266,7 @@ filegroup(
"//packages/kbn-doc-links:build_types",
"//packages/kbn-docs-utils:build_types",
"//packages/kbn-es-archiver:build_types",
"//packages/kbn-es-errors:build_types",
"//packages/kbn-es-query:build_types",
"//packages/kbn-eslint-plugin-imports:build_types",
"//packages/kbn-field-types:build_types",

View file

@ -0,0 +1,97 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")
PKG_DIRNAME = "kbn-es-errors"
PKG_REQUIRE_NAME = "@kbn/es-errors"
SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*",
],
)
SRCS = SOURCE_FILES
filegroup(
name = "srcs",
srcs = SRCS,
)
NPM_MODULE_EXTRA_FILES = [
"package.json",
]
RUNTIME_DEPS = [
"@npm//@elastic/elasticsearch"
]
TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//@elastic/elasticsearch"
]
jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)
ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)
ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
tsconfig = ":tsconfig",
)
js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)
pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)
filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)
pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)
filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)

View file

@ -0,0 +1,3 @@
# @kbn/es-errors
Utility functions and types for errors of the `@elastic/elasticsearch` library.

View file

@ -0,0 +1,13 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-es-errors'],
};

View file

@ -0,0 +1,7 @@
{
"name": "@kbn/es-errors",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0"
}

View file

@ -8,15 +8,26 @@
import { errors } from '@elastic/elasticsearch';
/** @public */
/**
* An unauthorized (401) error returned by elasticsearch
* @public
*/
export type UnauthorizedError = errors.ResponseError & {
statusCode: 401;
};
/**
* Checks if the provided `error` is an {@link errors.ResponseError | elasticsearch response error}
* @public
*/
export function isResponseError(error: unknown): error is errors.ResponseError {
return error instanceof errors.ResponseError;
}
/**
* Checks if the provided `error` is an {@link UnauthorizedError | elasticsearch unauthorized error}
* @public
*/
export function isUnauthorizedError(error: unknown): error is UnauthorizedError {
return isResponseError(error) && error.statusCode === 401;
}

View file

@ -0,0 +1,11 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export type { ElasticsearchErrorDetails } from './types';
export { isUnauthorizedError, isResponseError } from './errors';
export type { UnauthorizedError } from './errors';

View file

@ -0,0 +1,15 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
/**
* Detail information about an elasticsearch error.
* @public
*/
export interface ElasticsearchErrorDetails {
error?: { type: string; reason?: string };
}

View file

@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.bazel.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "target_types",
"rootDir": "src",
"stripInternal": false,
"types": [
"jest",
"node"
]
},
"include": [
"src/**/*"
]
}

View file

@ -14,7 +14,7 @@ import type {
} from '@elastic/transport';
import type { TransportOptions } from '@elastic/transport/lib/Transport';
import { Transport } from '@elastic/elasticsearch';
import { isUnauthorizedError } from './errors';
import { isUnauthorizedError } from '@kbn/es-errors';
import { InternalUnauthorizedErrorHandler, isRetryResult } from './retry_unauthorized';
type TransportClass = typeof Transport;

View file

@ -14,7 +14,6 @@ export type {
SearchResponse,
GetResponse,
DeleteDocumentResponse,
ElasticsearchErrorDetails,
} from './types';
export { ScopedClusterClient } from './scoped_cluster_client';
export type { IScopedClusterClient } from './scoped_cluster_client';
@ -33,4 +32,3 @@ export type {
UnauthorizedErrorHandlerToolkit,
UnauthorizedErrorHandler,
} from './retry_unauthorized';
export type { UnauthorizedError } from './errors';

View file

@ -11,7 +11,7 @@ import { stringify } from 'querystring';
import { errors, DiagnosticResult, RequestBody, Client } from '@elastic/elasticsearch';
import numeral from '@elastic/numeral';
import type { Logger } from '@kbn/logging';
import type { ElasticsearchErrorDetails } from './types';
import type { ElasticsearchErrorDetails } from '@kbn/es-errors';
import { getEcsResponseLog } from './get_ecs_response_log';
const convertQueryString = (qs: string | Record<string, any> | undefined): string => {

View file

@ -6,9 +6,9 @@
* Side Public License, v 1.
*/
import type { UnauthorizedError } from '@kbn/es-errors';
import type { SetAuthHeaders } from '../../http';
import { httpServerMock } from '../../http/http_server.mocks';
import type { UnauthorizedError } from './errors';
import {
createInternalErrorHandler,
isRetryResult,

View file

@ -7,9 +7,9 @@
*/
import { MaybePromise } from '@kbn/utility-types';
import { UnauthorizedError } from '@kbn/es-errors';
import { AuthHeaders, KibanaRequest, SetAuthHeaders, isRealRequest } from '../../http';
import { ScopeableRequest } from '../types';
import { UnauthorizedError } from './errors';
/**
* @public

View file

@ -120,10 +120,3 @@ export interface DeleteDocumentResponse {
type: string;
};
}
/**
* @public
*/
export interface ElasticsearchErrorDetails {
error?: { type: string; reason?: string };
}

View file

@ -38,7 +38,6 @@ export type {
ShardsResponse,
GetResponse,
DeleteDocumentResponse,
ElasticsearchErrorDetails,
// unauthorized error handler
UnauthorizedErrorHandlerOptions,
UnauthorizedErrorHandlerResultRetryParams,
@ -47,7 +46,6 @@ export type {
UnauthorizedErrorHandlerResult,
UnauthorizedErrorHandlerToolkit,
UnauthorizedErrorHandler,
UnauthorizedError,
} from './client';
export { getRequestDebugMeta, getErrorMessage } from './client';
export { pollEsNodesVersion } from './version_check/ensure_es_version';

View file

@ -13,9 +13,10 @@ import {
import typeDetect from 'type-detect';
import Boom from '@hapi/boom';
import * as stream from 'stream';
import { isResponseError as isElasticsearchResponseError } from '../../elasticsearch/client/errors';
import { ElasticsearchErrorDetails } from '../../elasticsearch';
import {
ElasticsearchErrorDetails,
isResponseError as isElasticsearchResponseError,
} from '@kbn/es-errors';
import {
HttpResponsePayload,

View file

@ -14,7 +14,7 @@ import type { Logger } from '@kbn/logging';
import {
isUnauthorizedError as isElasticsearchUnauthorizedError,
UnauthorizedError as EsNotAuthorizedError,
} from '../../elasticsearch/client/errors';
} from '@kbn/es-errors';
import { KibanaRequest, CoreKibanaRequest } from './request';
import {
KibanaResponseFactory,

View file

@ -146,7 +146,6 @@ export type {
GetResponse,
DeleteDocumentResponse,
ElasticsearchConfigPreboot,
ElasticsearchErrorDetails,
PollEsNodesVersionOptions,
UnauthorizedErrorHandlerOptions,
UnauthorizedErrorHandlerResultRetryParams,
@ -155,7 +154,6 @@ export type {
UnauthorizedErrorHandlerResult,
UnauthorizedErrorHandlerToolkit,
UnauthorizedErrorHandler,
UnauthorizedError,
} from './elasticsearch';
export type { IExternalUrlConfig, IExternalUrlPolicy } from './external_url';

View file

@ -6,9 +6,10 @@
* Side Public License, v 1.
*/
import { errors as esErrors } from '@elastic/elasticsearch';
import { get } from 'lodash';
import { ElasticsearchErrorDetails, isSupportedEsServer } from '../../../elasticsearch';
import { errors as esErrors } from '@elastic/elasticsearch';
import type { ElasticsearchErrorDetails } from '@kbn/es-errors';
import { isSupportedEsServer } from '../../../elasticsearch';
const responseErrors = {
isServiceUnavailable: (statusCode?: number) => statusCode === 503,

View file

@ -6,7 +6,7 @@
*/
/* eslint-disable max-classes-per-file */
import type { ElasticsearchErrorDetails } from '@kbn/core/server';
import type { ElasticsearchErrorDetails } from '@kbn/es-errors';
import { isESClientError } from './utils';

View file

@ -22,7 +22,6 @@ import type {
Logger,
LoggerFactory,
OnPreResponseToolkit,
UnauthorizedError,
UnauthorizedErrorHandler,
UnauthorizedErrorHandlerToolkit,
} from '@kbn/core/server';
@ -34,6 +33,7 @@ import {
httpServiceMock,
loggingSystemMock,
} from '@kbn/core/server/mocks';
import type { UnauthorizedError } from '@kbn/es-errors';
import type { PublicMethodsOf } from '@kbn/utility-types';
import type { AuthenticatedUser, SecurityLicense } from '../../common';

View file

@ -3277,6 +3277,10 @@
version "0.0.0"
uid ""
"@kbn/es-errors@link:bazel-bin/packages/kbn-es-errors":
version "0.0.0"
uid ""
"@kbn/es-query@link:bazel-bin/packages/kbn-es-query":
version "0.0.0"
uid ""
@ -6801,6 +6805,10 @@
version "0.0.0"
uid ""
"@types/kbn__es-errors@link:bazel-bin/packages/kbn-es-errors/npm_module_types":
version "0.0.0"
uid ""
"@types/kbn__es-query@link:bazel-bin/packages/kbn-es-query/npm_module_types":
version "0.0.0"
uid ""