From 6bc05c6566dc89aba0b5e7cfb1fd92d81ee650c1 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Thu, 28 Feb 2019 21:21:04 -0800 Subject: [PATCH 1/2] Add `await` to common router request handler --- .../call_with_request_factory.js | 11 +++++------ x-pack/server/lib/create_router/index.js | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js b/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js index 7359a831994f9..eaab7b2ab6c47 100644 --- a/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js +++ b/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js @@ -6,13 +6,12 @@ import { once } from 'lodash'; -const callWithRequest = once(server => { - const cluster = server.plugins.elasticsearch.getCluster('data'); - return cluster.callWithRequest; -}); - export const callWithRequestFactory = (server, request) => { return (...args) => { - return callWithRequest(server)(request, ...args); + const callWithRequest = server => { + const cluster = server.plugins.elasticsearch.getCluster('data'); + return cluster.callWithRequest; + }; + return once(callWithRequest)(server)(request, ...args); }; }; diff --git a/x-pack/server/lib/create_router/index.js b/x-pack/server/lib/create_router/index.js index 56353dc45fe74..74ec7dc936740 100644 --- a/x-pack/server/lib/create_router/index.js +++ b/x-pack/server/lib/create_router/index.js @@ -16,13 +16,13 @@ export const createRouter = (server, pluginId, apiBasePath = '') => { const requestHandler = (handler) => async (request, h) => { const callWithRequest = callWithRequestFactory(server, request); try { - return handler(request, callWithRequest, h); + return await handler(request, callWithRequest, h); } catch (err) { if (isEsError(err)) { - throw wrapEsError(err); + return wrapEsError(err); } - throw wrapUnknownError(err); + return wrapUnknownError(err); } }; From 253a2102cdca5dbd61743c1c49cfc96ec7f42faa Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Wed, 6 Mar 2019 12:27:49 -0800 Subject: [PATCH 2/2] Remove once() from common license checker and call with request factories --- .../call_with_request_factory.js | 9 ++------- .../license_pre_routing_factory.js | 5 +---- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js b/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js index eaab7b2ab6c47..a7703335f0c90 100644 --- a/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js +++ b/x-pack/server/lib/create_router/call_with_request_factory/call_with_request_factory.js @@ -4,14 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { once } from 'lodash'; - export const callWithRequestFactory = (server, request) => { + const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); return (...args) => { - const callWithRequest = server => { - const cluster = server.plugins.elasticsearch.getCluster('data'); - return cluster.callWithRequest; - }; - return once(callWithRequest)(server)(request, ...args); + return callWithRequest(request, ...args); }; }; diff --git a/x-pack/server/lib/create_router/license_pre_routing_factory/license_pre_routing_factory.js b/x-pack/server/lib/create_router/license_pre_routing_factory/license_pre_routing_factory.js index be0920d71806d..d545a15b24b98 100644 --- a/x-pack/server/lib/create_router/license_pre_routing_factory/license_pre_routing_factory.js +++ b/x-pack/server/lib/create_router/license_pre_routing_factory/license_pre_routing_factory.js @@ -4,12 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { once } from 'lodash'; import { wrapCustomError } from '../error_wrappers'; import { LICENSE_STATUS } from '../../../../common/constants'; export const licensePreRoutingFactory = (server, pluginId) => { - const licensePreRouting = () => { + return () => { // License checking and enable/disable logic const xpackMainPlugin = server.plugins.xpack_main; const licenseCheckResults = xpackMainPlugin.info.feature(pluginId).getLicenseCheckResults(); @@ -21,6 +20,4 @@ export const licensePreRoutingFactory = (server, pluginId) => { return null; }; - - return () => once(licensePreRouting)(); };