SDC Designers Integration - Part 1
[sdc.git] / catalog-ui / src / app / models / server-error-response.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 /**
22  * Created by ngordon on 7/27/2017.
23  */
24
25 import { Response } from '@angular/http';
26 import { SEVERITY, ServerErrors } from "../utils/constants";
27
28 export class ServerErrorResponse {
29
30     title: string;
31     message: string;
32     messageId: string;
33     status: number;
34     severity: SEVERITY;
35
36     constructor(response?: Response) {
37
38         if (response) {
39             let rejectionObj: any = {};
40             if (response.text().length) {
41                 let rejection = response.json();
42                 rejectionObj = rejection.serviceException || rejection.requestError && (rejection.requestError.serviceException || rejection.requestError.policyException);
43                 rejectionObj.text = this.getFormattedMessage(rejectionObj.text || ServerErrors.MESSAGE_ERROR, rejectionObj.variables);
44             }
45
46             this.title = ServerErrors.ERROR_TITLE;
47             this.message = rejectionObj.text || response.statusText || ServerErrors.DEFAULT_ERROR;
48             this.messageId = rejectionObj.messageId;
49             this.status = response.status;
50             this.severity = SEVERITY.ERROR;
51         }
52     }
53
54
55     private getFormattedMessage = (text: string, variables: Array<string>): string => { //OLD CODE
56         // Remove the "Error: " text at the begining
57         if (text.trim().indexOf("Error:") === 0) {
58             text = text.replace("Error:", "").trim();
59         }
60
61         //mshitrit DE199895 bug fix
62         let count: number = 0;
63         variables.forEach(function (item) {
64             variables[count] = item ? item.replace('<', '&lt').replace('>', '&gt') : '';
65             count++;
66         });
67
68         // Format the message in case has array to <ul><li>
69         text = text.replace(/\[%(\d+)\]/g, function (_, m) {
70             let tmp = [];
71             let list = variables[--m].split(";");
72             list.forEach(function (item) {
73                 tmp.push("<li>" + item + "</li>");
74             });
75             return "<ul>" + tmp.join("") + "</ul>";
76         });
77
78         // Format the message %1 %2
79         text = text.format(variables);
80
81         return text;
82
83     };
84 }