Skip to content

Commit

Permalink
Use code from legacy repo for type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Sep 7, 2020
1 parent 663bfac commit 8e26047
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import Boom from 'boom';
import { httpServerMock } from 'src/core/server/mocks';
import { createAppContextStartContractMock } from './mocks';

import { createAppContextStartContractMock } from '../mocks';
import { appContextService } from '../services';
import {
IngestManagerError,
RegistryError,
PackageNotFoundError,
defaultIngestErrorHandler,
} from './errors';
import { appContextService } from './services';
} from './index';

describe('defaultIngestErrorHandler', () => {
let mockContract: ReturnType<typeof createAppContextStartContractMock>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
* you may not use this file except in compliance with the Elastic License.
*/

/* eslint-disable max-classes-per-file */
import Boom, { isBoom } from 'boom';
import {
RequestHandlerContext,
KibanaRequest,
IKibanaResponse,
KibanaResponseFactory,
} from 'src/core/server';
import { appContextService } from './services';
import { errors as LegacyESErrors } from 'elasticsearch';
import { appContextService } from '../services';
import { IngestManagerError, RegistryError, PackageNotFoundError } from './index';

type IngestErrorHandler = (
params: IngestErrorHandlerParams
) => IKibanaResponse | Promise<IKibanaResponse>;

interface IngestErrorHandlerParams {
error: IngestManagerError | Boom | Error;
response: KibanaResponseFactory;
request?: KibanaRequest;
context?: RequestHandlerContext;
}

// unsure if this is correct. would prefer to use something "official"
// this type & guard are based on values observed while debugging https://github.com/elastic/kibana/issues/75862
// this type is based on BadRequest values observed while debugging https://github.com/elastic/kibana/issues/75862
interface LegacyESClientError {
statusCode: number;
message: string;
Expand All @@ -44,23 +43,9 @@ interface LegacyESClientError {
}

export const isLegacyESClientError = (error: any): error is LegacyESClientError => {
return (
'statusCode' in error &&
'message' in error &&
'body' in error &&
'path' in error &&
'query' in error &&
'response' in error
);
return error instanceof LegacyESErrors._Abstract;
};

export class IngestManagerError extends Error {
constructor(message?: string) {
super(message);
this.name = this.constructor.name; // for stack traces
}
}

const getHTTPResponseCode = (error: IngestManagerError): number => {
if (error instanceof RegistryError) {
return 502; // Bad Gateway
Expand All @@ -79,10 +64,10 @@ export const defaultIngestErrorHandler: IngestErrorHandler = async ({
const logger = appContextService.getLogger();
if (isLegacyESClientError(error)) {
// there was a problem communicating with ES (e.g. via `callCluster`)
// log full error
logger.error(error);
// only log the message
logger.error(error.message);
// return the endpoint that failed and the body it returned
const message = `${error.message} from ES at ${error.path}: ${error.response}`;
const message = `${error.message} response from ${error.path}: ${error.response}`;
return response.customError({
statusCode: error.statusCode,
body: { message },
Expand Down Expand Up @@ -116,9 +101,3 @@ export const defaultIngestErrorHandler: IngestErrorHandler = async ({
body: { message: error.message },
});
};

export class RegistryError extends IngestManagerError {}
export class RegistryConnectionError extends RegistryError {}
export class RegistryResponseError extends RegistryError {}
export class PackageNotFoundError extends IngestManagerError {}
export class PackageOutdatedError extends IngestManagerError {}
20 changes: 20 additions & 0 deletions x-pack/plugins/ingest_manager/server/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

/* eslint-disable max-classes-per-file */
export { defaultIngestErrorHandler } from './handlers';

export class IngestManagerError extends Error {
constructor(message?: string) {
super(message);
this.name = this.constructor.name; // for stack traces
}
}
export class RegistryError extends IngestManagerError {}
export class RegistryConnectionError extends RegistryError {}
export class RegistryResponseError extends RegistryError {}
export class PackageNotFoundError extends IngestManagerError {}
export class PackageOutdatedError extends IngestManagerError {}

0 comments on commit 8e26047

Please sign in to comment.