ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 4 | * 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import store from 'sdc-app/AppStore.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 17 | import React from 'react'; |
| 18 | import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 19 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 20 | import SubmitErrorResponse from 'nfvo-components/SubmitErrorResponse.jsx'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | |
| 22 | function 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 | |
| 32 | function parseATTExceptionObject(responseJSON) { |
| 33 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 46 | else if (responseJSON.uploadDataErrors) { |
| 47 | title = i18n('Error: Upload Data Error'); |
| 48 | msg = (<SubmitErrorResponse validationResponse={{uploadDataErrors: responseJSON.uploadDataErrors}} />); |
| 49 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 50 | else { |
| 51 | title = responseJSON.status; |
| 52 | msg = responseJSON.message; |
| 53 | } |
| 54 | return {title, msg}; |
| 55 | } |
| 56 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 57 | var errorResponseHandler = (error) => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 58 | let errorData; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 59 | if (error.data) { |
| 60 | errorData = parseATTExceptionObject(error.data); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 61 | } |
| 62 | else { |
| 63 | errorData = { |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 64 | title: error.statusText, |
| 65 | msg: error.responseText, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 66 | }; |
| 67 | } |
| 68 | store.dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | type: modalActionTypes.GLOBAL_MODAL_ERROR, |
| 70 | data: { |
| 71 | ...errorData |
| 72 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 73 | }); |
| 74 | }; |
| 75 | |
| 76 | export default errorResponseHandler; |