From 51c76205777a9a05783c781bad8c66e3eadef163 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Mon, 6 Nov 2017 15:08:24 +0100 Subject: [PATCH] Move 'parseError' to request lib --- .../src/main/js/apps/code/components/App.tsx | 8 ++------ .../src/main/js/apps/code/components/Search.tsx | 2 +- server/sonar-web/src/main/js/apps/code/utils.ts | 15 --------------- .../permission-templates/views/CreateView.js | 2 +- .../permission-templates/views/DeleteView.js | 2 +- .../permission-templates/views/UpdateView.js | 2 +- .../js/apps/permissions/global/store/actions.js | 2 +- .../js/apps/project-admin/key/BulkUpdate.js | 2 +- .../src/main/js/apps/project-admin/key/Key.js | 2 +- .../project-admin/links/views/CreationModal.js | 2 +- .../project-admin/links/views/DeletionModal.js | 2 +- .../js/apps/quality-gates/views/copy-view.js | 2 +- .../js/apps/quality-gates/views/create-view.js | 2 +- .../js/apps/quality-gates/views/delete-view.js | 2 +- .../views/gate-conditions-delete-view.js | 2 +- .../js/apps/quality-gates/views/rename-view.js | 2 +- .../js/apps/settings/components/EmailForm.js | 2 +- .../src/main/js/apps/settings/store/actions.js | 2 +- .../settings/store/encryptionPage/actions.js | 2 +- .../js/components/controls/FavoriteContainer.ts | 2 +- server/sonar-web/src/main/js/helpers/request.ts | 17 +++++++++++++++++ .../sonar-web/src/main/js/store/rootActions.js | 2 +- 22 files changed, 38 insertions(+), 40 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/code/components/App.tsx b/server/sonar-web/src/main/js/apps/code/components/App.tsx index a7a7432e3cd..77110f20481 100644 --- a/server/sonar-web/src/main/js/apps/code/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/code/components/App.tsx @@ -26,12 +26,8 @@ import { Component as CodeComponent } from '../types'; import SourceViewer from './../../../components/SourceViewer/SourceViewer'; import Search from './Search'; import ListFooter from '../../../components/controls/ListFooter'; -import { - retrieveComponentChildren, - retrieveComponent, - loadMoreChildren, - parseError -} from '../utils'; +import { retrieveComponentChildren, retrieveComponent, loadMoreChildren } from '../utils'; +import { parseError } from '../../../helpers/request'; import { addComponent, addComponentBreadcrumbs, clearBucket } from '../bucket'; import { getBranchName } from '../../../helpers/branches'; import { translate } from '../../../helpers/l10n'; diff --git a/server/sonar-web/src/main/js/apps/code/components/Search.tsx b/server/sonar-web/src/main/js/apps/code/components/Search.tsx index 3e666e19317..14539241c9a 100644 --- a/server/sonar-web/src/main/js/apps/code/components/Search.tsx +++ b/server/sonar-web/src/main/js/apps/code/components/Search.tsx @@ -22,7 +22,7 @@ import * as PropTypes from 'prop-types'; import * as classNames from 'classnames'; import Components from './Components'; import { getTree } from '../../../api/components'; -import { parseError } from '../utils'; +import { parseError } from '../../../helpers/request'; import { getProjectUrl } from '../../../helpers/urls'; import { Component } from '../types'; import SearchBox from '../../../components/controls/SearchBox'; diff --git a/server/sonar-web/src/main/js/apps/code/utils.ts b/server/sonar-web/src/main/js/apps/code/utils.ts index 9e8c7c3ceb6..8b705b6c339 100644 --- a/server/sonar-web/src/main/js/apps/code/utils.ts +++ b/server/sonar-web/src/main/js/apps/code/utils.ts @@ -28,7 +28,6 @@ import { } from './bucket'; import { Breadcrumb, Component } from './types'; import { getChildren, getComponent, getBreadcrumbs } from '../../api/components'; -import { translate } from '../../helpers/l10n'; const METRICS = [ 'ncloc', @@ -236,17 +235,3 @@ export function loadMoreChildren( return r; }); } - -/** Parse response of failed request */ -export function parseError(error: { response: Response }): Promise { - const DEFAULT_MESSAGE = translate('default_error_message'); - - try { - return error.response - .json() - .then(r => r.errors.map((error: any) => error.msg).join('. ')) - .catch(() => DEFAULT_MESSAGE); - } catch (ex) { - return Promise.resolve(DEFAULT_MESSAGE); - } -} diff --git a/server/sonar-web/src/main/js/apps/permission-templates/views/CreateView.js b/server/sonar-web/src/main/js/apps/permission-templates/views/CreateView.js index 70e80add995..11869ffcf47 100644 --- a/server/sonar-web/src/main/js/apps/permission-templates/views/CreateView.js +++ b/server/sonar-web/src/main/js/apps/permission-templates/views/CreateView.js @@ -19,7 +19,7 @@ */ import FormView from './FormView'; import { createPermissionTemplate } from '../../../api/permissions'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default FormView.extend({ sendRequest() { diff --git a/server/sonar-web/src/main/js/apps/permission-templates/views/DeleteView.js b/server/sonar-web/src/main/js/apps/permission-templates/views/DeleteView.js index 4304ab0b0ee..152d6eaff2e 100644 --- a/server/sonar-web/src/main/js/apps/permission-templates/views/DeleteView.js +++ b/server/sonar-web/src/main/js/apps/permission-templates/views/DeleteView.js @@ -20,7 +20,7 @@ import ModalForm from '../../../components/common/modal-form'; import { deletePermissionTemplate } from '../../../api/permissions'; import Template from '../templates/permission-templates-delete.hbs'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/permission-templates/views/UpdateView.js b/server/sonar-web/src/main/js/apps/permission-templates/views/UpdateView.js index 886cd53934b..20621cf4bb4 100644 --- a/server/sonar-web/src/main/js/apps/permission-templates/views/UpdateView.js +++ b/server/sonar-web/src/main/js/apps/permission-templates/views/UpdateView.js @@ -19,7 +19,7 @@ */ import FormView from './FormView'; import { updatePermissionTemplate } from '../../../api/permissions'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default FormView.extend({ sendRequest() { diff --git a/server/sonar-web/src/main/js/apps/permissions/global/store/actions.js b/server/sonar-web/src/main/js/apps/permissions/global/store/actions.js index 82dfc9e1b67..c25f252a9c2 100644 --- a/server/sonar-web/src/main/js/apps/permissions/global/store/actions.js +++ b/server/sonar-web/src/main/js/apps/permissions/global/store/actions.js @@ -19,7 +19,7 @@ */ // @flow import * as api from '../../../../api/permissions'; -import { parseError } from '../../../code/utils'; +import { parseError } from '../../../../helpers/request'; import { raiseError, REQUEST_HOLDERS, diff --git a/server/sonar-web/src/main/js/apps/project-admin/key/BulkUpdate.js b/server/sonar-web/src/main/js/apps/project-admin/key/BulkUpdate.js index 93996b99b12..ff7c49e772a 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/key/BulkUpdate.js +++ b/server/sonar-web/src/main/js/apps/project-admin/key/BulkUpdate.js @@ -24,7 +24,7 @@ import BulkUpdateForm from './BulkUpdateForm'; import BulkUpdateResults from './BulkUpdateResults'; import { translate, translateWithParameters } from '../../../helpers/l10n'; import { bulkChangeKey } from '../../../api/components'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; import { addGlobalErrorMessage, addGlobalSuccessMessage, diff --git a/server/sonar-web/src/main/js/apps/project-admin/key/Key.js b/server/sonar-web/src/main/js/apps/project-admin/key/Key.js index 18aa4ef3fc7..e94839f6b2e 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/key/Key.js +++ b/server/sonar-web/src/main/js/apps/project-admin/key/Key.js @@ -32,7 +32,7 @@ import { closeAllGlobalMessages, addGlobalSuccessMessage } from '../../../store/globalMessages/duck'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; import { reloadUpdateKeyPage } from './utils'; import RecentHistory from '../../../app/components/RecentHistory'; import { getProjectAdminProjectModules } from '../../../store/rootReducer'; diff --git a/server/sonar-web/src/main/js/apps/project-admin/links/views/CreationModal.js b/server/sonar-web/src/main/js/apps/project-admin/links/views/CreationModal.js index c0ed8620108..764bfded48b 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/links/views/CreationModal.js +++ b/server/sonar-web/src/main/js/apps/project-admin/links/views/CreationModal.js @@ -19,7 +19,7 @@ */ import ModalForm from '../../../../components/common/modal-form'; import Template from './CreationModalTemplate.hbs'; -import { parseError } from '../../../code/utils'; +import { parseError } from '../../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/project-admin/links/views/DeletionModal.js b/server/sonar-web/src/main/js/apps/project-admin/links/views/DeletionModal.js index 232857084c5..e04175c4f1b 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/links/views/DeletionModal.js +++ b/server/sonar-web/src/main/js/apps/project-admin/links/views/DeletionModal.js @@ -20,7 +20,7 @@ import ModalForm from '../../../../components/common/modal-form'; import Template from './DeletionModalTemplate.hbs'; import { deleteLink } from '../../../../api/projectLinks'; -import { parseError } from '../../../code/utils'; +import { parseError } from '../../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/quality-gates/views/copy-view.js b/server/sonar-web/src/main/js/apps/quality-gates/views/copy-view.js index 56e431a1d1c..a9b6da0d2f2 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/views/copy-view.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/views/copy-view.js @@ -20,7 +20,7 @@ import ModalForm from '../../../components/common/modal-form'; import Template from '../templates/quality-gate-form.hbs'; import { copyQualityGate } from '../../../api/quality-gates'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/quality-gates/views/create-view.js b/server/sonar-web/src/main/js/apps/quality-gates/views/create-view.js index 52857783c48..3580b45fcf6 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/views/create-view.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/views/create-view.js @@ -20,7 +20,7 @@ import ModalForm from '../../../components/common/modal-form'; import Template from '../templates/quality-gate-form.hbs'; import { createQualityGate } from '../../../api/quality-gates'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/quality-gates/views/delete-view.js b/server/sonar-web/src/main/js/apps/quality-gates/views/delete-view.js index 35968644518..3327a6d3a10 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/views/delete-view.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/views/delete-view.js @@ -20,7 +20,7 @@ import ModalForm from '../../../components/common/modal-form'; import Template from '../templates/quality-gates-delete.hbs'; import { deleteQualityGate } from '../../../api/quality-gates'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/quality-gates/views/gate-conditions-delete-view.js b/server/sonar-web/src/main/js/apps/quality-gates/views/gate-conditions-delete-view.js index 6ab82044087..f9f01acf285 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/views/gate-conditions-delete-view.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/views/gate-conditions-delete-view.js @@ -21,7 +21,7 @@ import ModalForm from '../../../components/common/modal-form'; import Template from '../templates/quality-gates-condition-delete.hbs'; import { deleteCondition } from '../../../api/quality-gates'; import { getLocalizedMetricName } from '../../../helpers/l10n'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/quality-gates/views/rename-view.js b/server/sonar-web/src/main/js/apps/quality-gates/views/rename-view.js index acc5f71c4d3..2614ea6af67 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/views/rename-view.js +++ b/server/sonar-web/src/main/js/apps/quality-gates/views/rename-view.js @@ -20,7 +20,7 @@ import ModalForm from '../../../components/common/modal-form'; import Template from '../templates/quality-gate-form.hbs'; import { renameQualityGate } from '../../../api/quality-gates'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; export default ModalForm.extend({ template: Template, diff --git a/server/sonar-web/src/main/js/apps/settings/components/EmailForm.js b/server/sonar-web/src/main/js/apps/settings/components/EmailForm.js index f8b2329d4af..7c2b8ff34e0 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/EmailForm.js +++ b/server/sonar-web/src/main/js/apps/settings/components/EmailForm.js @@ -21,7 +21,7 @@ import React from 'react'; import { connect } from 'react-redux'; import { translate, translateWithParameters } from '../../../helpers/l10n'; import { sendTestEmail } from '../../../api/settings'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; import { getCurrentUser } from '../../../store/rootReducer'; class EmailForm extends React.PureComponent { diff --git a/server/sonar-web/src/main/js/apps/settings/store/actions.js b/server/sonar-web/src/main/js/apps/settings/store/actions.js index 1e61966af34..6b8a6d9cdc0 100644 --- a/server/sonar-web/src/main/js/apps/settings/store/actions.js +++ b/server/sonar-web/src/main/js/apps/settings/store/actions.js @@ -26,7 +26,7 @@ import { import { receiveValues } from './values/actions'; import { receiveDefinitions } from './definitions/actions'; import { startLoading, stopLoading } from './settingsPage/loading/actions'; -import { parseError } from '../../code/utils'; +import { parseError } from '../../../helpers/request'; import { addGlobalErrorMessage, closeAllGlobalMessages } from '../../../store/globalMessages/duck'; import { passValidation, failValidation } from './settingsPage/validationMessages/actions'; import { cancelChange } from './settingsPage/changedValues/actions'; diff --git a/server/sonar-web/src/main/js/apps/settings/store/encryptionPage/actions.js b/server/sonar-web/src/main/js/apps/settings/store/encryptionPage/actions.js index 3c9ee71d934..0afb176767a 100644 --- a/server/sonar-web/src/main/js/apps/settings/store/encryptionPage/actions.js +++ b/server/sonar-web/src/main/js/apps/settings/store/encryptionPage/actions.js @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as api from '../../../../api/settings'; -import { parseError } from '../../../code/utils'; +import { parseError } from '../../../../helpers/request'; import { addGlobalErrorMessage, closeAllGlobalMessages diff --git a/server/sonar-web/src/main/js/components/controls/FavoriteContainer.ts b/server/sonar-web/src/main/js/components/controls/FavoriteContainer.ts index 087ace39dbb..59f6e07fef4 100644 --- a/server/sonar-web/src/main/js/components/controls/FavoriteContainer.ts +++ b/server/sonar-web/src/main/js/components/controls/FavoriteContainer.ts @@ -23,7 +23,7 @@ import { isFavorite } from '../../store/rootReducer'; import * as actionCreators from '../../store/favorites/duck'; import * as api from '../../api/favorites'; import { addGlobalErrorMessage } from '../../store/globalMessages/duck'; -import { parseError } from '../../apps/code/utils'; +import { parseError } from '../../helpers/request'; const addFavorite = (componentKey: string) => (dispatch: Function) => { // optimistic update diff --git a/server/sonar-web/src/main/js/helpers/request.ts b/server/sonar-web/src/main/js/helpers/request.ts index 6e955baeeac..3aa3a90248c 100644 --- a/server/sonar-web/src/main/js/helpers/request.ts +++ b/server/sonar-web/src/main/js/helpers/request.ts @@ -20,6 +20,7 @@ import { stringify } from 'querystring'; import { omitBy, isNil } from 'lodash'; import { getCookie } from './cookies'; +import { translate } from './l10n'; export function getCSRFTokenName(): string { return 'X-XSRF-TOKEN'; @@ -166,6 +167,22 @@ export function parseJSON(response: Response): Promise { return response.json(); } +/** + * Parse response of failed request + */ +export function parseError(error: { response: Response }): Promise { + const DEFAULT_MESSAGE = translate('default_error_message'); + + try { + return error.response + .json() + .then(r => r.errors.map((error: any) => error.msg).join('. ')) + .catch(() => DEFAULT_MESSAGE); + } catch (ex) { + return Promise.resolve(DEFAULT_MESSAGE); + } +} + /** * Shortcut to do a GET request and return response json */ diff --git a/server/sonar-web/src/main/js/store/rootActions.js b/server/sonar-web/src/main/js/store/rootActions.js index 3f41686b567..3775e1d0391 100644 --- a/server/sonar-web/src/main/js/store/rootActions.js +++ b/server/sonar-web/src/main/js/store/rootActions.js @@ -25,7 +25,7 @@ import { getAllMetrics } from '../api/metrics'; import { receiveLanguages } from './languages/actions'; import { receiveMetrics } from './metrics/actions'; import { addGlobalErrorMessage } from './globalMessages/duck'; -import { parseError } from '../apps/code/utils'; +import { parseError } from '../helpers/request'; import { setAppState } from './appState/duck'; import { receiveOrganizations } from './organizations/duck'; -- 2.39.5