Skip to content

Commit

Permalink
Export error handler type & add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Sep 3, 2020
1 parent 427df9c commit d7ad578
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
34 changes: 22 additions & 12 deletions x-pack/plugins/ingest_manager/server/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

/* eslint-disable max-classes-per-file */
import Boom from 'boom';
import Boom, { isBoom } from 'boom';
import {
RequestHandlerContext,
KibanaRequest,
Expand All @@ -14,6 +14,17 @@ import {
} from 'src/core/server';
import { appContextService } from './services';

type IngestErrorHandler = (
params: IngestErrorHandlerParams
) => IKibanaResponse | Promise<IKibanaResponse>;

interface IngestErrorHandlerParams {
error: IngestManagerError | Boom | Error;
response: KibanaResponseFactory;
request?: KibanaRequest;
context?: RequestHandlerContext;
}

export class IngestManagerError extends Error {
constructor(message?: string) {
super(message);
Expand All @@ -32,34 +43,33 @@ export const getHTTPResponseCode = (error: IngestManagerError): number => {
return 400; // Bad Request
};

interface IngestErrorHandlerParams {
error: IngestManagerError | Boom | Error;
response: KibanaResponseFactory;
request?: KibanaRequest;
context?: RequestHandlerContext;
}

export const defaultIngestErrorHandler = async ({
export const defaultIngestErrorHandler: IngestErrorHandler = async ({
error,
request,
response,
context,
}: IngestErrorHandlerParams): Promise<IKibanaResponse> => {
const logger = appContextService.getLogger();

// our "expected" errors
if (error instanceof IngestManagerError) {
// only log the message
logger.error(error.message);
return response.customError({
statusCode: getHTTPResponseCode(error),
body: { message: error.message },
});
}
if ('isBoom' in error) {

// handle any older Boom-based errors or the few places our app uses them
if (isBoom(error)) {
// only log the message
logger.error(error.output.payload.message);
return response.customError({
statusCode: error.output.statusCode,
body: { message: error.output.payload.message },
});
}

// not sure what type of error this is. log as much as possible
logger.error(error);
return response.customError({
statusCode: 500,
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ export const getFleetStatusHandler: RequestHandler = async (context, request, re
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down

0 comments on commit d7ad578

Please sign in to comment.