ilanap | 785dc1e | 2018-01-08 15:50:18 +0200 | [diff] [blame] | 1 | /*! |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame] | 2 | * Copyright © 2016-2018 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 | 785dc1e | 2018-01-08 15:50:18 +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 | 785dc1e | 2018-01-08 15:50:18 +0200 | [diff] [blame] | 12 | * 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 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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 18 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | } |
| 31 | |
ilanap | 785dc1e | 2018-01-08 15:50:18 +0200 | [diff] [blame] | 32 | function parseCatalogExceptionObject(responseJSON) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | let title, msg; |
| 34 | if ( |
| 35 | responseJSON.requestError && |
| 36 | responseJSON.requestError.policyException |
| 37 | ) { |
| 38 | title = 'Error: ' + responseJSON.requestError.policyException.messageId; |
| 39 | msg = responseJSON.requestError.policyException.text; |
| 40 | } else if ( |
| 41 | responseJSON.requestError && |
| 42 | responseJSON.requestError.serviceException |
| 43 | ) { |
| 44 | title = |
| 45 | 'Error: ' + responseJSON.requestError.serviceException.messageId; |
| 46 | msg = responseJSON.requestError.serviceException.text; |
| 47 | let { variables } = responseJSON.requestError.serviceException; |
| 48 | if (variables) { |
| 49 | msg = showVariablesInMessage(variables, msg); |
| 50 | } |
| 51 | } else if (responseJSON.uploadDataErrors) { |
| 52 | title = i18n('Error: Upload Data Error'); |
| 53 | msg = ( |
| 54 | <SubmitErrorResponse |
| 55 | validationResponse={{ |
| 56 | uploadDataErrors: responseJSON.uploadDataErrors |
| 57 | }} |
| 58 | /> |
| 59 | ); |
| 60 | } else { |
| 61 | title = responseJSON.status; |
| 62 | msg = responseJSON.message; |
| 63 | } |
| 64 | return { title, msg }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 65 | } |
| 66 | |
andre.schmid | 4594cba | 2022-01-25 19:38:32 +0000 | [diff] [blame] | 67 | const errorResponseHandler = error => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 68 | let errorData; |
| 69 | if (error.data) { |
| 70 | errorData = parseCatalogExceptionObject(error.data); |
| 71 | } else { |
| 72 | errorData = { |
| 73 | title: error.statusText, |
| 74 | msg: error.responseText ? error.responseText : i18n('GENERIC_ERROR') |
| 75 | }; |
| 76 | } |
| 77 | store.dispatch({ |
| 78 | type: modalActionTypes.GLOBAL_MODAL_ERROR, |
| 79 | data: { |
| 80 | ...errorData |
| 81 | } |
| 82 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | export default errorResponseHandler; |