Skip to content

Commit

Permalink
Add missing error handlers for deprecation logging route (#113109)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabarasaba authored Sep 27, 2021
1 parent ede315d commit 231ab17
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { kibanaResponseFactory } from 'src/core/server';
import { createMockRouter, MockRouter, routeHandlerContextMock } from './__mocks__/routes.mock';
import { createRequestMock } from './__mocks__/request.mock';
import { handleEsError } from '../shared_imports';

jest.mock('../lib/es_version_precheck', () => ({
versionCheckHandlerWrapper: (a: any) => a,
Expand All @@ -28,6 +29,7 @@ describe('deprecation logging API', () => {
mockRouter = createMockRouter();
routeDependencies = {
router: mockRouter,
lib: { handleEsError },
};
registerDeprecationLoggingRoutes(routeDependencies);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { versionCheckHandlerWrapper } from '../lib/es_version_precheck';
import { RouteDependencies } from '../types';
import { DEPRECATION_LOGS_INDEX } from '../../common/constants';

export function registerDeprecationLoggingRoutes({ router }: RouteDependencies) {
export function registerDeprecationLoggingRoutes({
router,
lib: { handleEsError },
}: RouteDependencies) {
router.get(
{
path: `${API_BASE_PATH}/deprecation_logging`,
Expand All @@ -32,8 +35,12 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies)
request,
response
) => {
const result = await getDeprecationLoggingStatus(client);
return response.ok({ body: result });
try {
const result = await getDeprecationLoggingStatus(client);
return response.ok({ body: result });
} catch (error) {
return handleEsError({ error, response });
}
}
)
);
Expand All @@ -57,10 +64,14 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies)
request,
response
) => {
const { isEnabled } = request.body as { isEnabled: boolean };
return response.ok({
body: await setDeprecationLogging(client, isEnabled),
});
try {
const { isEnabled } = request.body as { isEnabled: boolean };
return response.ok({
body: await setDeprecationLogging(client, isEnabled),
});
} catch (error) {
return handleEsError({ error, response });
}
}
)
);
Expand All @@ -84,28 +95,32 @@ export function registerDeprecationLoggingRoutes({ router }: RouteDependencies)
request,
response
) => {
const { body: indexExists } = await client.asCurrentUser.indices.exists({
index: DEPRECATION_LOGS_INDEX,
});
try {
const { body: indexExists } = await client.asCurrentUser.indices.exists({
index: DEPRECATION_LOGS_INDEX,
});

if (!indexExists) {
return response.ok({ body: { count: 0 } });
}
if (!indexExists) {
return response.ok({ body: { count: 0 } });
}

const { body } = await client.asCurrentUser.count({
index: DEPRECATION_LOGS_INDEX,
body: {
query: {
range: {
'@timestamp': {
gte: request.query.from,
const { body } = await client.asCurrentUser.count({
index: DEPRECATION_LOGS_INDEX,
body: {
query: {
range: {
'@timestamp': {
gte: request.query.from,
},
},
},
},
},
});
});

return response.ok({ body: { count: body.count } });
return response.ok({ body: { count: body.count } });
} catch (error) {
return handleEsError({ error, response });
}
}
)
);
Expand Down

0 comments on commit 231ab17

Please sign in to comment.