diff --git a/x-pack/plugins/ingest_manager/server/errors.ts b/x-pack/plugins/ingest_manager/server/errors.ts index 193a65fd8df7f..ee03b3faf79d1 100644 --- a/x-pack/plugins/ingest_manager/server/errors.ts +++ b/x-pack/plugins/ingest_manager/server/errors.ts @@ -4,26 +4,20 @@ * you may not use this file except in compliance with the Elastic License. */ +/* eslint-disable max-classes-per-file */ export class IngestManagerError extends Error { - public type: IngestManagerErrorType; - public message: string; - - constructor(type: IngestManagerErrorType, message: string) { + constructor(message?: string) { super(message); - this.type = type; - this.message = message; + this.name = this.constructor.name; // for stack traces } } export const getHTTPResponseCode = (error: IngestManagerError): number => { - switch (error.type) { - case IngestManagerErrorType.RegistryError: - return 502; // Bad Gateway - default: - return 400; // Bad Request + if (error instanceof RegistryError) { + return 502; // Bad Gateway + } else { + return 400; // Bad Request } }; -export enum IngestManagerErrorType { - RegistryError, -} +export class RegistryError extends IngestManagerError {} 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 6d039345e31c3..abf77ddddfd7a 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 @@ -6,7 +6,7 @@ import fetch, { Response } from 'node-fetch'; import { streamToString } from './streams'; -import { IngestManagerError, IngestManagerErrorType } from '../../../errors'; +import { RegistryError } from '../../../errors'; export async function getResponse(url: string): Promise { try { @@ -14,16 +14,12 @@ export async function getResponse(url: string): Promise { if (response.ok) { return response; } else { - throw new IngestManagerError( - IngestManagerErrorType.RegistryError, + throw new RegistryError( `Error connecting to package registry at ${url}: ${response.statusText}` ); } } catch (e) { - throw new IngestManagerError( - IngestManagerErrorType.RegistryError, - `Error connecting to package registry at ${url}: ${e.message}` - ); + throw new RegistryError(`Error connecting to package registry at ${url}: ${e.message}`); } }