DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / errormng / BaseException.java
1 package org.onap.sdc.dcae.errormng;
2
3 import com.fasterxml.jackson.annotation.JsonIgnore;
4 import com.google.gson.Gson;
5 import org.springframework.http.HttpStatus;
6 import org.springframework.web.client.HttpClientErrorException;
7
8 public class BaseException extends HttpClientErrorException {
9
10         private static Gson gson = new Gson();
11
12         protected RequestError requestError;
13
14         public RequestError getRequestError() {
15                 return requestError;
16         }
17
18         public void setRequestError(RequestError requestError) {
19                 this.requestError = requestError;
20         }
21
22         public BaseException(HttpClientErrorException theError) {
23                 super(theError.getStatusCode());
24                 String body = theError.getResponseBodyAsString();
25                 if (body != null) {
26                         requestError = extractRequestError(body);
27                 }
28         }
29
30         public BaseException(HttpStatus status, RequestError re){
31                 super(status);
32                 requestError = re;
33         }
34
35         private RequestError extractRequestError(String error) {
36                 ResponseFormat responseFormat = gson.fromJson(error, ResponseFormat.class);
37                 return responseFormat.getRequestError();
38         }
39
40         @JsonIgnore
41         public String getMessageId() {
42                 return requestError.getMessageId();
43         }
44
45         @JsonIgnore
46         public String[] getVariables() {
47                 return requestError.getVariables();
48         }
49
50         @JsonIgnore
51         public String getText(){
52                 return requestError.getText();
53         }
54
55         @Override
56         @JsonIgnore
57         public String getMessage() {
58                 return requestError.getFormattedMessage();
59         }
60
61 }