Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
57ef8f2
create saved object client that is native javascript
nreese Jun 29, 2018
062bf3e
fix savedObjectClient unit tests
nreese Jul 2, 2018
d086830
get saved object client from chrome when being used outside of angular
nreese Jul 2, 2018
56f92bb
update error handlers to pull status code from FetchError
nreese Jul 2, 2018
bda978b
add some debug messages to failing functional test
nreese Jul 2, 2018
1ebfc8a
revert changes to management/_objects
nreese Jul 3, 2018
f1629f4
Merge branch 'master' of https://github.com/elastic/kibana into saved…
nreese Jul 3, 2018
ce30939
add clicks to import done in import objects test
nreese Jul 3, 2018
dc8509f
take screenshots of test only failing in CI
nreese Jul 3, 2018
68a62bc
Merge branch 'master' of https://github.com/elastic/kibana into saved…
nreese Jul 3, 2018
773a0cd
remove functional test screenshot code since test is passing in CI now
nreese Jul 3, 2018
b87a31a
remove unused file, clean up saved_objects_client test to not use glo…
nreese Jul 5, 2018
bcce492
add body to kfetch error
nreese Jul 5, 2018
0eedca8
Merge branch 'master' of https://github.com/elastic/kibana into saved…
nreese Jul 10, 2018
79dbabe
update savedObjectClient.bulkCreate
nreese Jul 10, 2018
e295c79
add bulkCreate wrapper to SavedObjectsClientProvider
nreese Jul 10, 2018
1801eb0
Merge branch 'master' of https://github.com/elastic/kibana into saved…
nreese Jul 11, 2018
a8f5a1f
mark _createSavedObject and _processBatchQueue as private methods
nreese Jul 11, 2018
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
4 changes: 1 addition & 3 deletions src/core_plugins/kibana/public/dashboard/dashboard_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import * as filterActions from 'ui/doc_table/actions/filter';
import { FilterManagerProvider } from 'ui/filter_manager';
import { EmbeddableFactoriesRegistryProvider } from 'ui/embeddable/embeddable_factories_registry';
import { DashboardPanelActionsRegistryProvider } from 'ui/dashboard_panel_actions/dashboard_panel_actions_registry';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
import { timefilter } from 'ui/timefilter';

Expand Down Expand Up @@ -86,7 +85,6 @@ app.directive('dashboardApp', function ($injector) {

panelActionsStore.initializeFromRegistry(panelActionsRegistry);

const savedObjectsClient = Private(SavedObjectsClientProvider);
const visTypes = Private(VisTypesRegistryProvider);
$scope.getEmbeddableFactory = panelType => embeddableFactories.byName[panelType];

Expand Down Expand Up @@ -391,7 +389,7 @@ app.directive('dashboardApp', function ($injector) {
const isLabsEnabled = config.get('visualize:enableLabs');
const listingLimit = config.get('savedObjects:listingLimit');

showAddPanel(savedObjectsClient, dashboardStateManager.addNewPanel, addNewVis, listingLimit, isLabsEnabled, visTypes);
showAddPanel(chrome.getSavedObjectsClient(), dashboardStateManager.addNewPanel, addNewVis, listingLimit, isLabsEnabled, visTypes);
};
updateViewMode(dashboardStateManager.getViewMode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/

import React from 'react';
import chrome from 'ui/chrome';
import { render, unmountComponentAtNode } from 'react-dom';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { FetchFieldsProvider } from '../lib/fetch_fields';
import { extractIndexPatterns } from '../lib/extract_index_patterns';

function ReactEditorControllerProvider(Private, config) {
const fetchFields = Private(FetchFieldsProvider);
const savedObjectsClient = Private(SavedObjectsClientProvider);
const savedObjectsClient = chrome.getSavedObjectsClient();

class ReactEditorController {
constructor(el, savedObj) {
Expand Down
28 changes: 28 additions & 0 deletions src/ui/public/chrome/api/saved_object_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SavedObjectsClient } from '../../saved_objects';

export function initSavedObjectClient(chrome) {
const savedObjectClient = new SavedObjectsClient();

chrome.getSavedObjectsClient = function () {
return savedObjectClient;
};
}
2 changes: 2 additions & 0 deletions src/ui/public/chrome/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import translationsApi from './api/translations';
import { initChromeXsrfApi } from './api/xsrf';
import { initUiSettingsApi } from './api/ui_settings';
import { initLoadingCountApi } from './api/loading_count';
import { initSavedObjectClient } from './api/saved_object_client';

export const chrome = {};
const internals = _.defaults(
Expand All @@ -62,6 +63,7 @@ const internals = _.defaults(
);

initUiSettingsApi(chrome);
initSavedObjectClient(chrome);
appsApi(chrome, internals);
initChromeXsrfApi(chrome, internals);
initChromeNavApi(chrome, internals);
Expand Down
12 changes: 8 additions & 4 deletions src/ui/public/courier/saved_object/__tests__/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ describe('Saved Object', function () {
return savedObject.init();
}

const mock409FetchError = {
res: { status: 409 }
};

beforeEach(ngMock.module(
'kibana',
StubIndexPatternsApiClientModule,
Expand All @@ -104,15 +108,15 @@ describe('Saved Object', function () {
describe('with confirmOverwrite', function () {
function stubConfirmOverwrite() {
window.confirm = sinon.stub().returns(true);
sinon.stub(esDataStub, 'create').returns(BluebirdPromise.reject({ status: 409 }));
sinon.stub(esDataStub, 'create').returns(BluebirdPromise.reject(mock409FetchError));
}

describe('when true', function () {
it('requests confirmation and updates on yes response', function () {
stubESResponse(getMockedDocResponse('myId'));
return createInitializedSavedObject({ type: 'dashboard', id: 'myId' }).then(savedObject => {
const createStub = sinon.stub(savedObjectsClientStub, 'create');
createStub.onFirstCall().returns(BluebirdPromise.reject({ statusCode: 409 }));
createStub.onFirstCall().returns(BluebirdPromise.reject(mock409FetchError));
createStub.onSecondCall().returns(BluebirdPromise.resolve({ id: 'myId' }));

stubConfirmOverwrite();
Expand All @@ -135,7 +139,7 @@ describe('Saved Object', function () {
return createInitializedSavedObject({ type: 'dashboard', id: 'HI' }).then(savedObject => {
window.confirm = sinon.stub().returns(false);

sinon.stub(savedObjectsClientStub, 'create').returns(BluebirdPromise.reject({ statusCode: 409 }));
sinon.stub(savedObjectsClientStub, 'create').returns(BluebirdPromise.reject(mock409FetchError));

savedObject.lastSavedTitle = 'original title';
savedObject.title = 'new title';
Expand All @@ -154,7 +158,7 @@ describe('Saved Object', function () {
return createInitializedSavedObject({ type: 'dashboard', id: 'myId' }).then(savedObject => {
stubConfirmOverwrite();

sinon.stub(savedObjectsClientStub, 'create').returns(BluebirdPromise.reject({ statusCode: 409 }));
sinon.stub(savedObjectsClientStub, 'create').returns(BluebirdPromise.reject(mock409FetchError));

return savedObject.save({ confirmOverwrite: true })
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/courier/saved_object/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export function SavedObjectProvider(Promise, Private, Notifier, confirmModalProm
return savedObjectsClient.create(esType, source, { id: this.id })
.catch(err => {
// record exists, confirm overwriting
if (_.get(err, 'statusCode') === 409) {
if (_.get(err, 'res.status') === 409) {
const confirmMessage = `Are you sure you want to overwrite ${this.title}?`;

return confirmModalPromise(confirmMessage, { confirmButtonText: `Overwrite ${this.getDisplayName()}` })
Expand Down
24 changes: 8 additions & 16 deletions src/ui/public/error_auto_create_index/error_auto_create_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,20 @@
import { get } from 'lodash';

import uiRoutes from '../routes';
import { KbnUrlProvider } from '../url';

import './error_auto_create_index.less';
import template from './error_auto_create_index.html';

uiRoutes
.when('/error/action.auto_create_index', { template });

export function ErrorAutoCreateIndexProvider(Private, Promise) {
const kbnUrl = Private(KbnUrlProvider);

return new (class ErrorAutoCreateIndex {
test(error) {
return (
error.statusCode === 503 &&
get(error, 'body.code') === 'ES_AUTO_CREATE_INDEX_ERROR'
);
}
export function isAutoCreateIndexError(error) {
return (
get(error, 'res.status') === 503 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the original code incorrect, in which we were comparing error.statusCode?

Copy link
Contributor Author

@nreese nreese Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code used the error produced in saved_object_client _request method. But kfetch creates its own error type. Just needed to update to handle the error thrown by kfetch.

get(error, 'body.code') === 'ES_AUTO_CREATE_INDEX_ERROR'
);
}

takeover() {
kbnUrl.change('/error/action.auto_create_index');
return Promise.halt();
}
});
export function showAutoCreateIndexErrorPage() {
window.location.hash = '/error/action.auto_create_index';
}
109 changes: 109 additions & 0 deletions src/ui/public/error_auto_create_index/error_auto_create_index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

jest.mock('../chrome', () => ({
addBasePath: path => `myBase/${path}`,
}));
jest.mock('../metadata', () => ({
metadata: {
version: 'my-version',
},
}));

import fetchMock from 'fetch-mock';
import { kfetch } from 'ui/kfetch';
import { isAutoCreateIndexError } from './error_auto_create_index';

describe('isAutoCreateIndexError correctly handles FetchError thrown by kfetch', () => {
describe('404', () => {
beforeEach(() => {
fetchMock.post({
matcher: '*',
response: {
status: 404,
},
});
});
afterEach(() => fetchMock.restore());

test('should return false', async () => {
let gotError = false;
Copy link
Member

@sorenlouv sorenlouv Aug 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nreese stumbled over this while updating kfetch. fyi expect.assertions(1) is very handy in this exact scenario (https://jest-bot.github.io/jest/docs/expect.html#expectassertionsnumber)

try {
await kfetch({ method: 'POST', pathname: 'my/path' });
} catch (fetchError) {
gotError = true;
expect(isAutoCreateIndexError(fetchError)).toBe(false);
}

expect(gotError).toBe(true);
});
});

describe('503 error that is not ES_AUTO_CREATE_INDEX_ERROR', () => {
beforeEach(() => {
fetchMock.post({
matcher: '*',
response: {
status: 503,
},
});
});
afterEach(() => fetchMock.restore());

test('should return false', async () => {
let gotError = false;
try {
await kfetch({ method: 'POST', pathname: 'my/path' });
} catch (fetchError) {
gotError = true;
expect(isAutoCreateIndexError(fetchError)).toBe(false);
}

expect(gotError).toBe(true);
});
});

describe('503 error that is ES_AUTO_CREATE_INDEX_ERROR', () => {
beforeEach(() => {
fetchMock.post({
matcher: '*',
response: {
body: {
code: 'ES_AUTO_CREATE_INDEX_ERROR'
},
status: 503,
},
});
});
afterEach(() => fetchMock.restore());

test('should return true', async () => {
let gotError = false;
try {
await kfetch({ method: 'POST', pathname: 'my/path' });
} catch (fetchError) {
gotError = true;
expect(isAutoCreateIndexError(fetchError)).toBe(true);
}

expect(gotError).toBe(true);
});
});
});

2 changes: 1 addition & 1 deletion src/ui/public/error_auto_create_index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export { ErrorAutoCreateIndexProvider } from './error_auto_create_index';
export { isAutoCreateIndexError, showAutoCreateIndexErrorPage } from './error_auto_create_index';
11 changes: 9 additions & 2 deletions src/ui/public/kfetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { metadata } from '../metadata';
import { merge } from 'lodash';

class FetchError extends Error {
constructor(res) {
constructor(res, body) {
super(res.statusText);
this.res = res;
this.body = body;
Error.captureStackTrace(this, FetchError);
}
}
Expand Down Expand Up @@ -59,7 +60,13 @@ export async function kfetch(fetchOptions, kibanaOptions) {
const res = await fetch(fullUrl, combinedFetchOptions);

if (!res.ok) {
throw new FetchError(res);
let body;
try {
body = await res.json();
} catch (err) {
// ignore error, may not be able to get body for response that is not ok
}
throw new FetchError(res, body);
}

return res.json();
Expand Down
Loading