Fix bugs in attribute outputs page
[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 import { ServerErrors } from '../utils/constants';
25 import '../utils/prototypes';
26
27 export class ServerErrorResponse {
28
29     title: string;
30     message: string;
31     messageId: string;
32     status: number;
33     ecompRequestId: string;
34
35     constructor(response?: any, isNg1Response?: boolean) {
36         if (response) {
37             if (isNg1Response) {
38                 // Shall handle the case where this error response is generated from the NG1 http interceptor
39                 this.setValuesByRejectionObject(response, response.data);
40             } else {
41                 // Shall handle NG5 http error responses
42                 this.setValuesByRejectionObject(response, response.error);
43             }
44         }
45     }
46
47     private setValuesByRejectionObject(response: any, errorResponseBody: any) {
48         let rejectionObj: any = {};
49
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;
55             return;
56         }
57
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;
67             } else {
68                 rejectionObj.text = response.error;
69             }
70         }
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;
76     }
77
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();
82         }
83
84         // mshitrit DE199895 bug fix
85         let count: number = 0;
86         variables.forEach( (item) => {
87             variables[count] = item ? item.replace('<', '&lt').replace('>', '&gt') : '';
88             count++;
89         });
90
91         // Format the message in case has array to <ul><li>
92         text = text.replace(/\[%(\d+)\]/g, (_, m) => {
93             const tmp = [];
94             const list = variables[--m].split(';');
95             list.forEach((item) => {
96                 tmp.push('<li>' + item + '</li>');
97             });
98             return '<ul>' + tmp.join('') + '</ul>';
99         });
100
101         // Format the message %1 %2
102         text = text.format(variables);
103
104         return text;
105
106     }
107 }