Skip to content

Commit

Permalink
[Ingest Manager] Improve server-side error handling (#67278)
Browse files Browse the repository at this point in the history
* Use IngestManagerError instead of Boom errors.

* Extend Error and simplify.

* Add registry url to error messages.

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
skh and elasticmachine authored May 27, 2020
1 parent 5e8374c commit 9a241f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
29 changes: 29 additions & 0 deletions x-pack/plugins/ingest_manager/server/errors.ts
Original file line number Diff line number Diff line change
@@ -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,
}
8 changes: 8 additions & 0 deletions x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
* 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 {
const response = await fetch(url);
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}`
);
}
}

Expand Down

0 comments on commit 9a241f3

Please sign in to comment.