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

[Upgrade Assistant] Add missing error handlers for deprecation logging route #113109

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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