blob: 13dfb1f0ab0c935576b05a09b7be83530e496af8 [file] [log] [blame]
ilanap785dc1e2018-01-08 15:50:18 +02001/*!
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
AviZi280f8012017-06-09 02:39:56 +03007 *
ilanap785dc1e2018-01-08 15:50:18 +02008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
ilanap785dc1e2018-01-08 15:50:18 +020012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import store from 'sdc-app/AppStore.js';
AviZi280f8012017-06-09 02:39:56 +030017import React from 'react';
18import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
19import i18n from 'nfvo-utils/i18n/i18n.js';
20import SubmitErrorResponse from 'nfvo-components/SubmitErrorResponse.jsx';
Michael Landoefa037d2017-02-19 12:57:33 +020021
22function showVariablesInMessage(variables, msg) {
23 let regex;
24 variables.forEach((value, index) => {
25 value = value.replace(';', ',');
26 regex = new RegExp('\'\%' + (index + 1) + '\'');
27 msg = msg.replace(regex, value);
28 });
29 return msg;
30}
31
ilanap785dc1e2018-01-08 15:50:18 +020032function parseCatalogExceptionObject(responseJSON) {
Michael Landoefa037d2017-02-19 12:57:33 +020033 let title, msg;
34 if (responseJSON.requestError && responseJSON.requestError.policyException) {
35 title = 'Error: ' + responseJSON.requestError.policyException.messageId;
36 msg = responseJSON.requestError.policyException.text;
37 }
38 else if (responseJSON.requestError && responseJSON.requestError.serviceException) {
39 title = 'Error: ' + responseJSON.requestError.serviceException.messageId;
40 msg = responseJSON.requestError.serviceException.text;
41 let {variables} = responseJSON.requestError.serviceException;
42 if (variables) {
43 msg = showVariablesInMessage(variables, msg);
44 }
45 }
AviZi280f8012017-06-09 02:39:56 +030046 else if (responseJSON.uploadDataErrors) {
47 title = i18n('Error: Upload Data Error');
48 msg = (<SubmitErrorResponse validationResponse={{uploadDataErrors: responseJSON.uploadDataErrors}} />);
49 }
Michael Landoefa037d2017-02-19 12:57:33 +020050 else {
51 title = responseJSON.status;
52 msg = responseJSON.message;
53 }
54 return {title, msg};
55}
56
ilanap1965d162018-01-04 11:34:59 +020057var errorResponseHandler = (error) => {
Michael Landoefa037d2017-02-19 12:57:33 +020058 let errorData;
ilanap1965d162018-01-04 11:34:59 +020059 if (error.data) {
ilanap785dc1e2018-01-08 15:50:18 +020060 errorData = parseCatalogExceptionObject(error.data);
Michael Landoefa037d2017-02-19 12:57:33 +020061 }
62 else {
63 errorData = {
ilanap1965d162018-01-04 11:34:59 +020064 title: error.statusText,
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020065 msg: error.responseText ? error.responseText : i18n('GENERIC_ERROR'),
Michael Landoefa037d2017-02-19 12:57:33 +020066 };
67 }
68 store.dispatch({
AviZi280f8012017-06-09 02:39:56 +030069 type: modalActionTypes.GLOBAL_MODAL_ERROR,
70 data: {
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020071 ...errorData
AviZi280f8012017-06-09 02:39:56 +030072 }
Michael Landoefa037d2017-02-19 12:57:33 +020073 });
74};
75
76export default errorResponseHandler;