re base code
[sdc.git] / catalog-ui / src / app / services / http-error-interceptor.ts
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 'use strict';
22 import {IServerMessageModalModel} from "../view-models/modals/message-modal/message-server-modal/server-message-modal-view-model";
23 import {SEVERITY} from "../utils/constants";
24 import 'app/utils/prototypes.ts';
25
26 export class HttpErrorInterceptor {
27     public static $inject = ['$injector', '$q'];
28
29     public static Factory($injector:ng.auto.IInjectorService, $q:angular.IQService) {
30         return new HttpErrorInterceptor($injector, $q);
31     }
32
33     constructor(private $injector:ng.auto.IInjectorService, private $q:angular.IQService) {
34     }
35
36     public formatMessageArrays = (message:string, variables:Array<string>)=> {
37         return message.replace(/\[%(\d+)\]/g, function (_, m) {
38             let tmp = [];
39             let list = variables[--m].split(";");
40             list.forEach(function (item) {
41                 tmp.push("<li>" + item + "</li>");
42             });
43             return "<ul>" + tmp.join("") + "</ul>";
44         });
45     };
46
47     public responseError = (rejection:any)=> {
48
49         let text:string;
50         let variables;
51         let messageId:string = "";
52         let isKnownException = false;
53
54         if (rejection.data && rejection.data.serviceException) {
55             text = rejection.data.serviceException.text;
56             variables = rejection.data.serviceException.variables;
57             messageId = rejection.data.serviceException.messageId;
58             isKnownException = true;
59         } else if (rejection.data && rejection.data.requestError && rejection.data.requestError.serviceException) {
60             text = rejection.data.requestError.serviceException.text;
61             variables = rejection.data.requestError.serviceException.variables;
62             messageId = rejection.data.requestError.serviceException.messageId;
63             isKnownException = true;
64         } else if (rejection.data && rejection.data.requestError && rejection.data.requestError.policyException) {
65             text = rejection.data.requestError.policyException.text;
66             variables = rejection.data.requestError.policyException.variables;
67             messageId = rejection.data.requestError.policyException.messageId;
68             isKnownException = true;
69         } else if (rejection.data) {
70             text = 'Wrong error format from server';
71             console.error(text);
72             isKnownException = false;
73         }
74
75         let data:IServerMessageModalModel;
76         if (isKnownException) {
77             // Remove the "Error: " text at the begining
78             if (text.trim().indexOf("Error:") === 0) {
79                 text = text.replace("Error:", "").trim();
80             }
81
82             //mshitrit DE199895 bug fix
83             let count:number = 0;
84             variables.forEach(function (item) {
85                 variables[count] = item ? item.replace('<', '&lt').replace('>', '&gt') : '';
86                 count++;
87             });
88
89             // Format the message in case has array to <ul><li>
90             text = this.formatMessageArrays(text, variables);
91
92             // Format the message %1 %2
93             text = text.format(variables);
94
95             // Need to inject the MessageService manually to prevent circular componentsToUpgrade (because MessageService use $templateCache that use $http).
96             data = {
97                 title: 'Error',
98                 message: text,
99                 messageId: messageId,
100                 status: rejection.status,
101                 severity: SEVERITY.ERROR
102             };
103         } else {
104             // Need to inject the MessageService manually to prevent circular componentsToUpgrade (because MessageService use $templateCache that use $http).
105             data = {
106                 title: 'Error',
107                 message: rejection.status !== -1 ? rejection.statusText : "Error getting response from server",
108                 messageId: messageId,
109                 status: rejection.status,
110                 severity: SEVERITY.ERROR
111             };
112         }
113
114         let modalsHandler = this.$injector.get('ModalsHandler');
115         modalsHandler.openServerMessageModal(data);
116
117         return this.$q.reject(rejection);
118     }
119 }