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

[Reporting] Remove Boom #116289

Merged
merged 4 commits into from
Oct 28, 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
11 changes: 9 additions & 2 deletions x-pack/plugins/reporting/server/routes/lib/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import Boom from '@hapi/boom';
import { i18n } from '@kbn/i18n';
import { KibanaRequest, KibanaResponseFactory } from 'kibana/server';
import { ReportingCore } from '../..';
import { API_BASE_URL } from '../../../common/constants';
Expand Down Expand Up @@ -153,7 +154,13 @@ export class RequestHandler {
});
}

// unknown error, can't convert to 4xx
throw err;
return this.res.customError({
statusCode: 500,
body:
err?.message ||
i18n.translate('xpack.reporting.errorHandler.unknownError', {
defaultMessage: 'Unknown error',
}),
});
}
}
30 changes: 30 additions & 0 deletions x-pack/plugins/reporting/server/routes/management/jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ describe('GET /api/reporting/jobs/download', () => {
await supertest(httpSetup.server.listener).get('/api/reporting/jobs/download/poo').expect(401);
});

it(`returns job's info`, async () => {
mockEsClient.search.mockResolvedValueOnce({
body: getHits({
jobtype: 'base64EncodedJobType',
payload: {}, // payload is irrelevant
}),
} as any);

registerJobInfoRoutes(core);

await server.start();

await supertest(httpSetup.server.listener).get('/api/reporting/jobs/info/test').expect(200);
});

it(`returns 403 if a user cannot view a job's info`, async () => {
mockEsClient.search.mockResolvedValueOnce({
body: getHits({
jobtype: 'customForbiddenJobType',
payload: {}, // payload is irrelevant
}),
} as any);

registerJobInfoRoutes(core);

await server.start();

await supertest(httpSetup.server.listener).get('/api/reporting/jobs/info/test').expect(403);
});

it('when a job is incomplete', async () => {
mockEsClient.search.mockResolvedValueOnce({
body: getHits({
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/reporting/server/routes/management/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import Boom from '@hapi/boom';
import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { ReportingCore } from '../../';
import { ROUTE_TAG_CAN_REDIRECT } from '../../../../security/server';
import { API_BASE_URL } from '../../../common/constants';
Expand Down Expand Up @@ -115,7 +115,12 @@ export function registerJobInfoRoutes(reporting: ReportingCore) {
const { jobtype: jobType } = result;

if (!jobTypes.includes(jobType)) {
throw Boom.unauthorized(`Sorry, you are not authorized to view ${jobType} info`);
return res.forbidden({
body: i18n.translate('xpack.reporting.jobsQuery.infoError.unauthorizedErrorMessage', {
defaultMessage: 'Sorry, you are not authorized to view {jobType} info',
values: { jobType },
}),
});
}

return res.ok({
Expand Down