[Ingest Manager] Improve server-side error handling (#67278)

* Use IngestManagerError instead of Boom errors.

* Extend Error and simplify.

* Add registry url to error messages.

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Sonja Krause-Harder 2020-05-27 08:14:42 +02:00 committed by GitHub
parent 5e8374c1e7
commit 9a241f32ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 3 deletions

View file

@ -0,0 +1,29 @@
/*
* 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 extends Error {
public type: IngestManagerErrorType;
public message: string;
constructor(type: IngestManagerErrorType, message: string) {
super(message);
this.type = type;
this.message = message;
}
}
export const getHTTPResponseCode = (error: IngestManagerError): number => {
switch (error.type) {
case IngestManagerErrorType.RegistryError:
return 502; // Bad Gateway
default:
return 400; // Bad Request
}
};
export enum IngestManagerErrorType {
RegistryError,
}

View file

@ -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.message);
return response.customError({
statusCode: getHTTPResponseCode(e),
body: { message: e.message },
});
}
if (e.isBoom) {
logger.error(e.output.payload.message);
return response.customError({

View file

@ -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<Response> {
try {
@ -14,10 +14,16 @@ export async function getResponse(url: string): Promise<Response> {
if (response.ok) {
return response;
} else {
throw new Boom(response.statusText, { statusCode: response.status });
throw new IngestManagerError(
IngestManagerErrorType.RegistryError,
`Error connecting to package registry at ${url}: ${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 at ${url}: ${e.message}`
);
}
}