From 7d54314d74c32cd2dda1b2a647cbcfa4c03376ea Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 5 Aug 2019 09:35:22 -0600 Subject: [PATCH 1/5] Remove timeout from fetch. Falls back to 30 second timeout on server-side indexing call --- .../file_upload/public/util/http_service.js | 44 ++++--------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/x-pack/legacy/plugins/file_upload/public/util/http_service.js b/x-pack/legacy/plugins/file_upload/public/util/http_service.js index 44a6a0b31c7a6..164fd12751e0b 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/http_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/http_service.js @@ -4,16 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ - - // service for interacting with the server import chrome from 'ui/chrome'; import { addSystemApiHeader } from 'ui/system_api'; import { i18n } from '@kbn/i18n'; -const FETCH_TIMEOUT = 10000; - export async function http(options) { if(!(options && options.url)) { throw( @@ -40,38 +36,14 @@ export async function http(options) { if (body !== null) { payload.body = body; } - return await fetchWithTimeout(url, payload); + return await doFetch(url, payload); } -async function fetchWithTimeout(url, payload) { - let timedOut = false; - - return new Promise(function (resolve, reject) { - const timeout = setTimeout(function () { - timedOut = true; - reject(new Error( - i18n.translate('xpack.fileUpload.httpService.requestTimedOut', - { defaultMessage: 'Request timed out' })) - ); - }, FETCH_TIMEOUT); - - fetch(url, payload) - .then(resp => { - clearTimeout(timeout); - if (!timedOut) { - resolve(resp); - } - }) - .catch(function (err) { - reject(err); - if (timedOut) return; - }); - }).then(resp => resp.json()) - .catch(function (err) { - console.error( - i18n.translate('xpack.fileUpload.httpService.fetchError', { - defaultMessage: 'Error performing fetch: {error}', - values: { error: err.message } - })); - }); +async function doFetch(url, payload) { + return fetch(url, payload) + .then(resp => resp.json()) + .catch(err => i18n.translate('xpack.fileUpload.httpService.fetchError', { + defaultMessage: 'Error performing fetch: {error}', + values: { error: err.message } + })); } From fcdd702ee5bd72e193fb0e48860ca95ae0311fe9 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 5 Aug 2019 10:21:51 -0600 Subject: [PATCH 2/5] Handle when indexing request is a success but every doc failed --- .../layers/sources/client_file_source/geojson_file_source.js | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js index ed6c06f58b6ad..efb8c6f2e4829 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js @@ -53,6 +53,7 @@ export class GeojsonFileSource extends AbstractVectorSource { return (indexResponses = {}) => { const { indexDataResp, indexPatternResp } = indexResponses; if (!(indexDataResp && indexDataResp.success) || + indexDataResp.failures.length === indexDataResp.docCount || !(indexPatternResp && indexPatternResp.success)) { importErrorHandler(indexResponses); return; From 435a9f0ed7f3eec215284ca45d62252bb4bc249d Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 5 Aug 2019 11:02:19 -0600 Subject: [PATCH 3/5] Return failures array from catch --- .../plugins/file_upload/public/util/http_service.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x-pack/legacy/plugins/file_upload/public/util/http_service.js b/x-pack/legacy/plugins/file_upload/public/util/http_service.js index 164fd12751e0b..3429823cf6166 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/http_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/http_service.js @@ -42,8 +42,11 @@ export async function http(options) { async function doFetch(url, payload) { return fetch(url, payload) .then(resp => resp.json()) - .catch(err => i18n.translate('xpack.fileUpload.httpService.fetchError', { - defaultMessage: 'Error performing fetch: {error}', - values: { error: err.message } + .catch(err => ({ + failures: [ + i18n.translate('xpack.fileUpload.httpService.fetchError', { + defaultMessage: 'Error performing fetch: {error}', + values: { error: err.message } + })] })); } From 7a2e1f82fc1f2b219b78c1d96cd55e53f3090a0f Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 8 Aug 2019 09:05:22 -0600 Subject: [PATCH 4/5] Review feedback --- .../plugins/file_upload/public/util/http_service.js | 13 +++++++++---- .../client_file_source/geojson_file_source.js | 9 ++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/x-pack/legacy/plugins/file_upload/public/util/http_service.js b/x-pack/legacy/plugins/file_upload/public/util/http_service.js index 3429823cf6166..5808b8e30afe1 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/http_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/http_service.js @@ -40,13 +40,18 @@ export async function http(options) { } async function doFetch(url, payload) { - return fetch(url, payload) - .then(resp => resp.json()) - .catch(err => ({ + let resp; + let failures; + try { + resp = await fetch(url, payload); + } catch(err) { + failures = { failures: [ i18n.translate('xpack.fileUpload.httpService.fetchError', { defaultMessage: 'Error performing fetch: {error}', values: { error: err.message } })] - })); + }; + } + return resp ? resp.json() : failures; } diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js index efb8c6f2e4829..e750f1a33688b 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/geojson_file_source.js @@ -52,9 +52,12 @@ export class GeojsonFileSource extends AbstractVectorSource { ) => { return (indexResponses = {}) => { const { indexDataResp, indexPatternResp } = indexResponses; - if (!(indexDataResp && indexDataResp.success) || - indexDataResp.failures.length === indexDataResp.docCount || - !(indexPatternResp && indexPatternResp.success)) { + + const indexCreationFailed = !(indexDataResp && indexDataResp.success); + const allDocsFailed = indexDataResp.failures.length === indexDataResp.docCount; + const indexPatternCreationFailed = !(indexPatternResp && indexPatternResp.success); + + if (indexCreationFailed || allDocsFailed || indexPatternCreationFailed) { importErrorHandler(indexResponses); return; } From 2b9b5708c52b37ee28395e43227db36bfd272b10 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 8 Aug 2019 13:43:02 -0600 Subject: [PATCH 5/5] Review feedback --- .../plugins/file_upload/public/util/http_service.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/x-pack/legacy/plugins/file_upload/public/util/http_service.js b/x-pack/legacy/plugins/file_upload/public/util/http_service.js index 5808b8e30afe1..26d46cecb0e51 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/http_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/http_service.js @@ -40,12 +40,11 @@ export async function http(options) { } async function doFetch(url, payload) { - let resp; - let failures; try { - resp = await fetch(url, payload); + const resp = await fetch(url, payload); + return resp.json(); } catch(err) { - failures = { + return { failures: [ i18n.translate('xpack.fileUpload.httpService.fetchError', { defaultMessage: 'Error performing fetch: {error}', @@ -53,5 +52,4 @@ async function doFetch(url, payload) { })] }; } - return resp ? resp.json() : failures; }