Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / ExceptionWithRequestInfo.java
1 package org.onap.vid.aai;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.springframework.http.HttpMethod;
5
6 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
7
8 public class ExceptionWithRequestInfo extends RuntimeException {
9
10     private final HttpMethod httpMethod;
11     private final String requestedUrl;
12     private final Integer httpCode;
13     private final String rawData;
14
15     public ExceptionWithRequestInfo(HttpMethod httpMethod, String requestedUrl, String rawData, Integer httpCode, Throwable cause) {
16         super(toMessage(httpMethod, requestedUrl, cause), cause);
17         this.httpMethod = httpMethod;
18         this.requestedUrl = requestedUrl;
19         this.rawData = rawData;
20         this.httpCode = httpCode;
21     }
22
23     public ExceptionWithRequestInfo(HttpMethod httpMethod, String requestedUrl, Throwable cause) {
24         this(httpMethod, requestedUrl, null, null, cause);
25     }
26
27     public String getRequestedUrl() {
28         return requestedUrl;
29     }
30
31     public String getRawData() {
32         return rawData;
33     }
34
35     public HttpMethod getHttpMethod() {
36         return httpMethod;
37     }
38
39     public Integer getHttpCode() {
40         return httpCode;
41     }
42
43     private static String toMessage(HttpMethod httpMethod, String requestedUrl, Throwable cause) {
44         if (StringUtils.isEmpty(requestedUrl)) {
45             return String.valueOf(cause);
46         } else {
47             return "" +
48                     "Exception while handling " +
49                     defaultIfNull(httpMethod, "request").toString() +
50                     " " + requestedUrl +
51                     ": " + (cause == null ? "null" : cause.toString());
52         }
53     }
54 }