Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame^] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | import store from 'sdc-app/AppStore.js'; |
| 22 | import NotificationConstants from 'nfvo-components/notifications/NotificationConstants.js'; |
| 23 | |
| 24 | function showVariablesInMessage(variables, msg) { |
| 25 | let regex; |
| 26 | variables.forEach((value, index) => { |
| 27 | value = value.replace(';', ','); |
| 28 | regex = new RegExp('\'\%' + (index + 1) + '\''); |
| 29 | msg = msg.replace(regex, value); |
| 30 | }); |
| 31 | return msg; |
| 32 | } |
| 33 | |
| 34 | function parseATTExceptionObject(responseJSON) { |
| 35 | let title, msg; |
| 36 | if (responseJSON.requestError && responseJSON.requestError.policyException) { |
| 37 | title = 'Error: ' + responseJSON.requestError.policyException.messageId; |
| 38 | msg = responseJSON.requestError.policyException.text; |
| 39 | } |
| 40 | else if (responseJSON.requestError && responseJSON.requestError.serviceException) { |
| 41 | title = 'Error: ' + responseJSON.requestError.serviceException.messageId; |
| 42 | msg = responseJSON.requestError.serviceException.text; |
| 43 | let {variables} = responseJSON.requestError.serviceException; |
| 44 | if (variables) { |
| 45 | msg = showVariablesInMessage(variables, msg); |
| 46 | } |
| 47 | } |
| 48 | else { |
| 49 | title = responseJSON.status; |
| 50 | msg = responseJSON.message; |
| 51 | } |
| 52 | return {title, msg}; |
| 53 | } |
| 54 | |
| 55 | var errorResponseHandler = (xhr/*, textStatus, errorThrown*/) => { |
| 56 | let errorData; |
| 57 | if (xhr.responseJSON) { |
| 58 | errorData = parseATTExceptionObject(xhr.responseJSON); |
| 59 | } |
| 60 | else { |
| 61 | errorData = { |
| 62 | title: xhr.statusText, |
| 63 | msg: xhr.responseText |
| 64 | }; |
| 65 | } |
| 66 | store.dispatch({ |
| 67 | type: NotificationConstants.NOTIFY_ERROR, |
| 68 | data: {...errorData} |
| 69 | }); |
| 70 | }; |
| 71 | |
| 72 | export default errorResponseHandler; |