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 * Created by ngordon on 7/27/2017.
25 import { Response } from '@angular/http';
26 import { SEVERITY, ServerErrors } from "../utils/constants";
28 export class ServerErrorResponse {
36 constructor(response?: 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);
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;
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();
61 //mshitrit DE199895 bug fix
62 let count: number = 0;
63 variables.forEach(function (item) {
64 variables[count] = item ? item.replace('<', '<').replace('>', '>') : '';
68 // Format the message in case has array to <ul><li>
69 text = text.replace(/\[%(\d+)\]/g, function (_, m) {
71 let list = variables[--m].split(";");
72 list.forEach(function (item) {
73 tmp.push("<li>" + item + "</li>");
75 return "<ul>" + tmp.join("") + "</ul>";
78 // Format the message %1 %2
79 text = text.format(variables);