From e4795b90a99c190c58dfda7045fce324ceef246d Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 25 May 2020 15:31:16 +0200 Subject: [PATCH 1/3] Use IngestManagerError instead of Boom errors. --- .../plugins/ingest_manager/server/errors.ts | 36 +++++++++++++++++++ .../server/routes/setup/handlers.ts | 8 +++++ .../server/services/epm/registry/requests.ts | 12 +++++-- 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/ingest_manager/server/errors.ts diff --git a/x-pack/plugins/ingest_manager/server/errors.ts b/x-pack/plugins/ingest_manager/server/errors.ts new file mode 100644 index 0000000000000..a89b18e9b7930 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/errors.ts @@ -0,0 +1,36 @@ +/* + * 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 class IngestManagerError { + private type: IngestManagerErrorType; + private message: string; + + constructor(type: IngestManagerErrorType, message: string) { + this.type = type; + this.message = message; + } + + public getType = (): IngestManagerErrorType => { + return this.type; + }; + + public getMessage = (): string => { + return this.message; + }; +} + +export const getHTTPResponseCode = (error: IngestManagerError): number => { + switch (error.getType()) { + case IngestManagerErrorType.RegistryError: + return 502; // Bad Gateway + default: + return 400; // Bad Request + } +}; + +export enum IngestManagerErrorType { + RegistryError, +} diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index 8bc80c69ce9b2..474c72be702d8 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -9,6 +9,7 @@ import { outputService, appContextService } from '../../services'; import { GetFleetStatusResponse } from '../../../common'; import { setupIngestManager, setupFleet } from '../../services/setup'; import { PostFleetSetupRequestSchema } from '../../types'; +import { IngestManagerError, getHTTPResponseCode } from '../../errors'; export const getFleetStatusHandler: RequestHandler = async (context, request, response) => { const soClient = context.core.savedObjects.client; @@ -81,6 +82,13 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request body: { isInitialized: true }, }); } catch (e) { + if (e instanceof IngestManagerError) { + logger.error(e.getMessage()); + return response.customError({ + statusCode: getHTTPResponseCode(e), + body: { message: e.getMessage() }, + }); + } if (e.isBoom) { logger.error(e.output.payload.message); return response.customError({ diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts index 93e475cbc5956..632670e9ef096 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import Boom from 'boom'; import fetch, { Response } from 'node-fetch'; import { streamToString } from './streams'; +import { IngestManagerError, IngestManagerErrorType } from '../../../errors'; export async function getResponse(url: string): Promise { try { @@ -14,10 +14,16 @@ export async function getResponse(url: string): Promise { if (response.ok) { return response; } else { - throw new Boom(response.statusText, { statusCode: response.status }); + throw new IngestManagerError( + IngestManagerErrorType.RegistryError, + `Error connecting to package registry: ${response.statusText}` + ); } } catch (e) { - throw new Boom(`Error connecting to package registry: ${e.message}`, { statusCode: 502 }); + throw new IngestManagerError( + IngestManagerErrorType.RegistryError, + `Error connecting to package registry: ${e.message}` + ); } } From 4719bdb14e11e4a18bff470173b68f54512c1ea4 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 25 May 2020 17:11:30 +0200 Subject: [PATCH 2/3] Extend Error and simplify. --- x-pack/plugins/ingest_manager/server/errors.ts | 17 +++++------------ .../server/routes/setup/handlers.ts | 4 ++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/errors.ts b/x-pack/plugins/ingest_manager/server/errors.ts index a89b18e9b7930..193a65fd8df7f 100644 --- a/x-pack/plugins/ingest_manager/server/errors.ts +++ b/x-pack/plugins/ingest_manager/server/errors.ts @@ -4,26 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ -export class IngestManagerError { - private type: IngestManagerErrorType; - private message: string; +export class IngestManagerError extends Error { + public type: IngestManagerErrorType; + public message: string; constructor(type: IngestManagerErrorType, message: string) { + super(message); this.type = type; this.message = message; } - - public getType = (): IngestManagerErrorType => { - return this.type; - }; - - public getMessage = (): string => { - return this.message; - }; } export const getHTTPResponseCode = (error: IngestManagerError): number => { - switch (error.getType()) { + switch (error.type) { case IngestManagerErrorType.RegistryError: return 502; // Bad Gateway default: diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index 474c72be702d8..4a0054b247131 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -83,10 +83,10 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request }); } catch (e) { if (e instanceof IngestManagerError) { - logger.error(e.getMessage()); + logger.error(e.message); return response.customError({ statusCode: getHTTPResponseCode(e), - body: { message: e.getMessage() }, + body: { message: e.message }, }); } if (e.isBoom) { From b8a1088af073494be9708ceb4c5208b75cf7ed82 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Tue, 26 May 2020 11:35:41 +0200 Subject: [PATCH 3/3] Add registry url to error messages. --- .../ingest_manager/server/services/epm/registry/requests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts index 632670e9ef096..6d039345e31c3 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts @@ -16,13 +16,13 @@ export async function getResponse(url: string): Promise { } else { throw new IngestManagerError( IngestManagerErrorType.RegistryError, - `Error connecting to package registry: ${response.statusText}` + `Error connecting to package registry at ${url}: ${response.statusText}` ); } } catch (e) { throw new IngestManagerError( IngestManagerErrorType.RegistryError, - `Error connecting to package registry: ${e.message}` + `Error connecting to package registry at ${url}: ${e.message}` ); } }