2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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';
26 export class HttpErrorInterceptor {
27 public static $inject = ['$injector', '$q'];
29 public static Factory($injector:ng.auto.IInjectorService, $q:angular.IQService) {
30 return new HttpErrorInterceptor($injector, $q);
33 constructor(private $injector:ng.auto.IInjectorService, private $q:angular.IQService) {
36 public formatMessageArrays = (message:string, variables:Array<string>)=> {
37 return message.replace(/\[%(\d+)\]/g, function (_, m) {
39 let list = variables[--m].split(";");
40 list.forEach(function (item) {
41 tmp.push("<li>" + item + "</li>");
43 return "<ul>" + tmp.join("") + "</ul>";
47 public responseError = (rejection:any)=> {
51 let messageId:string = "";
52 let isKnownException = false;
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';
72 isKnownException = false;
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();
82 //mshitrit DE199895 bug fix
84 variables.forEach(function (item) {
85 variables[count] = item ? item.replace('<', '<').replace('>', '>') : '';
89 // Format the message in case has array to <ul><li>
90 text = this.formatMessageArrays(text, variables);
92 // Format the message %1 %2
93 text = text.format(variables);
95 // Need to inject the MessageService manually to prevent circular componentsToUpgrade (because MessageService use $templateCache that use $http).
100 status: rejection.status,
101 severity: SEVERITY.ERROR
104 // Need to inject the MessageService manually to prevent circular componentsToUpgrade (because MessageService use $templateCache that use $http).
107 message: rejection.status !== -1 ? rejection.statusText : "Error getting response from server",
108 messageId: messageId,
109 status: rejection.status,
110 severity: SEVERITY.ERROR
114 let modalsHandler = this.$injector.get('ModalsHandler');
115 modalsHandler.openServerMessageModal(data);
117 return this.$q.reject(rejection);