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.
24 import { ServerErrors } from '../utils/constants';
25 import '../utils/prototypes';
27 export class ServerErrorResponse {
33 ecompRequestId: string;
35 constructor(response?: any, isNg1Response?: boolean) {
38 // Shall handle the case where this error response is generated from the NG1 http interceptor
39 this.setValuesByRejectionObject(response, response.data);
41 // Shall handle NG5 http error responses
42 this.setValuesByRejectionObject(response, response.error);
47 private setValuesByRejectionObject(response: any, errorResponseBody: any) {
48 let rejectionObj: any = {};
50 // If it is an internal server error, we dont want to expose anything to the user, just display a default error an return
51 if (response.status === 500) {
52 this.title = ServerErrors.ERROR_TITLE;
53 this.message = ServerErrors.DEFAULT_ERROR;
54 this.status = response.status;
58 if (errorResponseBody) {
59 if (errorResponseBody.requestError || errorResponseBody.serviceException) {
60 rejectionObj = errorResponseBody.serviceException || errorResponseBody.requestError.serviceException || errorResponseBody.requestError.policyException;
61 rejectionObj.text = this.getFormattedMessage(rejectionObj.text || ServerErrors.MESSAGE_ERROR, rejectionObj.variables);
62 } else if (errorResponseBody.type === 'application/octet-stream') {
63 rejectionObj.text = 'Error downloading file';
64 rejectionObj.title = ServerErrors.DOWNLOAD_ERROR;
65 } else if (errorResponseBody.message) {
66 rejectionObj.text = response.error.message;
68 rejectionObj.text = response.error;
71 this.title = rejectionObj.title || ServerErrors.ERROR_TITLE;
72 this.message = rejectionObj.text || response.statusText || ServerErrors.DEFAULT_ERROR;
73 this.messageId = rejectionObj.messageId;
74 this.status = response.status;
75 this.ecompRequestId = rejectionObj.ecompRequestId;
78 private getFormattedMessage = (text: string, variables: string[]): string => {
79 // Remove the "Error: " text at the beginning
80 if (text.trim().indexOf('Error:') === 0) {
81 text = text.replace('Error:', '').trim();
84 // mshitrit DE199895 bug fix
85 let count: number = 0;
86 variables.forEach( (item) => {
87 variables[count] = item ? item.replace('<', '<').replace('>', '>') : '';
91 // Format the message in case has array to <ul><li>
92 text = text.replace(/\[%(\d+)\]/g, (_, m) => {
94 const list = variables[--m].split(';');
95 list.forEach((item) => {
96 tmp.push('<li>' + item + '</li>');
98 return '<ul>' + tmp.join('') + '</ul>';
101 // Format the message %1 %2
102 text = text.format(variables);