Skip to content

Commit 222d607

Browse files
[Painless Lab] Update server to use new es-js client (#88704) (#89206)
1 parent 8f31cdf commit 222d607

6 files changed

Lines changed: 87 additions & 13 deletions

File tree

src/plugins/es_ui_shared/__packages_do_not_import__/errors/handle_es_error.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ import { IKibanaResponse, KibanaResponseFactory } from 'kibana/server';
1313
interface EsErrorHandlerParams {
1414
error: ApiError;
1515
response: KibanaResponseFactory;
16+
handleCustomError?: () => IKibanaResponse<any>;
1617
}
1718

1819
/*
1920
* For errors returned by the new elasticsearch js client.
2021
*/
21-
export const handleEsError = ({ error, response }: EsErrorHandlerParams): IKibanaResponse => {
22+
export const handleEsError = ({
23+
error,
24+
response,
25+
handleCustomError,
26+
}: EsErrorHandlerParams): IKibanaResponse => {
2227
// error.name is slightly better in terms of performance, since all errors now have name property
2328
if (error.name === 'ResponseError') {
29+
// The consumer may sometimes want to provide a custom response
30+
if (typeof handleCustomError === 'function') {
31+
return handleCustomError();
32+
}
33+
2434
const { statusCode, body } = error as ResponseError;
2535
return response.customError({
2636
statusCode,

x-pack/plugins/painless_lab/server/routes/api/execute.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema';
77

88
import { API_BASE_PATH } from '../../../common/constants';
99
import { RouteDependencies } from '../../types';
10-
import { isEsError } from '../../shared_imports';
10+
import { handleEsError } from '../../shared_imports';
1111

1212
const bodySchema = schema.string();
1313

@@ -23,23 +23,24 @@ export function registerExecuteRoute({ router, license }: RouteDependencies) {
2323
const body = req.body;
2424

2525
try {
26-
const callAsCurrentUser = ctx.core.elasticsearch.legacy.client.callAsCurrentUser;
27-
const response = await callAsCurrentUser('scriptsPainlessExecute', {
26+
const client = ctx.core.elasticsearch.client.asCurrentUser;
27+
const response = await client.scriptsPainlessExecute({
2828
body,
2929
});
3030

3131
return res.ok({
32-
body: response,
32+
body: response.body,
3333
});
34-
} catch (e) {
35-
if (isEsError(e)) {
36-
// Assume invalid painless script was submitted
37-
// Return 200 with error object
34+
} catch (error) {
35+
// Assume invalid painless script was submitted
36+
// Return 200 with error object
37+
const handleCustomError = () => {
3838
return res.ok({
39-
body: e.body,
39+
body: error.body,
4040
});
41-
}
42-
return res.internalError({ body: e });
41+
};
42+
43+
return handleEsError({ error, response: res, handleCustomError });
4344
}
4445
})
4546
);

x-pack/plugins/painless_lab/server/shared_imports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export { isEsError } from '../../../../src/plugins/es_ui_shared/server';
7+
export { handleEsError } from '../../../../src/plugins/es_ui_shared/server';

x-pack/test/api_integration/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
3434
loadTestFile(require.resolve('./lists'));
3535
loadTestFile(require.resolve('./upgrade_assistant'));
3636
loadTestFile(require.resolve('./searchprofiler'));
37+
loadTestFile(require.resolve('./painless_lab'));
3738
});
3839
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { FtrProviderContext } from '../../ftr_provider_context';
8+
9+
export default function ({ loadTestFile }: FtrProviderContext) {
10+
describe('Painless Lab', () => {
11+
loadTestFile(require.resolve('./painless_lab'));
12+
});
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import expect from '@kbn/expect';
8+
import { FtrProviderContext } from '../../ftr_provider_context';
9+
10+
const API_BASE_PATH = '/api/painless_lab';
11+
12+
export default function ({ getService }: FtrProviderContext) {
13+
const supertest = getService('supertest');
14+
15+
describe('Painless Lab', function () {
16+
describe('Execute', () => {
17+
it('should execute a valid painless script', async () => {
18+
const script =
19+
'"{\\n \\"script\\": {\\n \\"source\\": \\"return true;\\",\\n \\"params\\": {\\n \\"string_parameter\\": \\"string value\\",\\n \\"number_parameter\\": 1.5,\\n \\"boolean_parameter\\": true\\n}\\n }\\n}"';
20+
21+
const { body } = await supertest
22+
.post(`${API_BASE_PATH}/execute`)
23+
.set('kbn-xsrf', 'xxx')
24+
.set('Content-Type', 'application/json;charset=UTF-8')
25+
.send(script)
26+
.expect(200);
27+
28+
expect(body).to.eql({
29+
result: 'true',
30+
});
31+
});
32+
33+
it('should return error response for invalid painless script', async () => {
34+
const invalidScript =
35+
'"{\\n \\"script\\": {\\n \\"source\\": \\"foobar\\",\\n \\"params\\": {\\n \\"string_parameter\\": \\"string value\\",\\n \\"number_parameter\\": 1.5,\\n \\"boolean_parameter\\": true\\n}\\n }\\n}"';
36+
37+
const { body } = await supertest
38+
.post(`${API_BASE_PATH}/execute`)
39+
.set('kbn-xsrf', 'xxx')
40+
.set('Content-Type', 'application/json;charset=UTF-8')
41+
.send(invalidScript)
42+
.expect(200);
43+
44+
expect(body.error).to.not.be(undefined);
45+
expect(body.error.reason).to.eql('compile error');
46+
});
47+
});
48+
});
49+
}

0 commit comments

Comments
 (0)