Skip to content

Commit

Permalink
Add default Error handler. Try on /*setup & /epm/*
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Sep 3, 2020
1 parent 2db7895 commit df19026
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 65 deletions.
39 changes: 39 additions & 0 deletions x-pack/plugins/ingest_manager/server/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/

/* eslint-disable max-classes-per-file */
import Boom from 'boom';
import { RequestHandlerContext, KibanaRequest, KibanaResponseFactory } from 'src/core/server';
import { appContextService } from './services';

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

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

export const defaultIngestErrorHandler = async ({
error,
request,
response,
context,
}: IngestErrorHandler) => {
const logger = appContextService.getLogger();
if (error instanceof IngestManagerError) {
logger.error(error.message);
return response.customError({
statusCode: getHTTPResponseCode(error),
body: { message: error.message },
});
}
if ('isBoom' in error) {
logger.error(error.output.payload.message);
return response.customError({
statusCode: error.output.statusCode,
body: { message: error.output.payload.message },
});
}
logger.error(error);
return response.customError({
statusCode: 500,
body: { message: error.message },
});
};

export class RegistryError extends IngestManagerError {}
export class RegistryConnectionError extends RegistryError {}
export class RegistryResponseError extends RegistryError {}
Expand Down
50 changes: 13 additions & 37 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
getLimitedPackages,
getInstallationObject,
} from '../../services/epm/packages';
import { IngestManagerError, getHTTPResponseCode } from '../../errors';
import { IngestManagerError, getHTTPResponseCode, defaultIngestErrorHandler } from '../../errors';
import { splitPkgKey } from '../../services/epm/registry';

export const getCategoriesHandler: RequestHandler<
Expand All @@ -45,11 +45,8 @@ export const getCategoriesHandler: RequestHandler<
response: res,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -69,11 +66,8 @@ export const getListHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -87,11 +81,8 @@ export const getLimitedListHandler: 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 All @@ -112,11 +103,8 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
customResponseObj.headers = { 'Content-Type': contentType };
}
return response.custom(customResponseObj);
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand All @@ -135,11 +123,8 @@ export const getInfoHandler: RequestHandler<TypeOf<typeof GetInfoRequestSchema.p
response: res,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -203,16 +188,7 @@ export const deletePackageHandler: RequestHandler<TypeOf<
response: res,
};
return response.ok({ body });
} catch (e) {
if (e.isBoom) {
return response.customError({
statusCode: e.output.statusCode,
body: { message: e.output.payload.message },
});
}
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
34 changes: 6 additions & 28 deletions x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { outputService, appContextService } from '../../services';
import { GetFleetStatusResponse, PostIngestSetupResponse } from '../../../common';
import { setupIngestManager, setupFleet } from '../../services/setup';
import { PostFleetSetupRequestSchema } from '../../types';
import { IngestManagerError, getHTTPResponseCode } from '../../errors';
import { defaultIngestErrorHandler } from '../../errors';

export const getFleetStatusHandler: RequestHandler = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
Expand Down Expand Up @@ -70,44 +70,22 @@ export const createFleetSetupHandler: RequestHandler<
return response.ok({
body: { isInitialized: true },
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

export const ingestManagerSetupHandler: RequestHandler = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const logger = appContextService.getLogger();

try {
const body: PostIngestSetupResponse = { isInitialized: true };
await setupIngestManager(soClient, callCluster);
return response.ok({
body,
});
} 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({
statusCode: e.output.statusCode,
body: { message: e.output.payload.message },
});
}
logger.error(e.message);
logger.error(e.stack);
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

0 comments on commit df19026

Please sign in to comment.