[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:
parent
5e8374c1e7
commit
9a241f32ea
3 changed files with 46 additions and 3 deletions
29
x-pack/plugins/ingest_manager/server/errors.ts
Normal file
29
x-pack/plugins/ingest_manager/server/errors.ts
Normal 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,
|
||||
}
|
|
@ -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({
|
||||
|
|
|
@ -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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue