Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions x-pack/plugins/ingest_manager/server/errors.ts
Original file line number Diff line number Diff line change
@@ -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 {
skh marked this conversation as resolved.
Show resolved Hide resolved
private type: IngestManagerErrorType;
private message: string;

constructor(type: IngestManagerErrorType, message: string) {
this.type = type;
this.message = message;
}

public getType = (): IngestManagerErrorType => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think instead of relaying on type having all of our errors extending IngestManagerError

class RegistryError extends IngestManagerError {
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #66688 (comment)

We can do that. We don't use that pattern (inheritance + instanceof) at the moment in ingest manager code, and I didn't want to introduce it when it isn't needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a really good pattern for Error (inheritance + instanceof) and we can keep the getStatusCode method so this stay an internal to this error module, curious to have other thoughts here, maybe @jfsiii?

But it's really personal preference for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be removed once we stop using Boom elsewhere. This handler is currently in an in-between state where it should capture both Boom and IngestManagerError errors

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: ${response.statusText}`
skh marked this conversation as resolved.
Show resolved Hide resolved
);
}
} 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}`
);
}
}

Expand Down