summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/errormng/BaseException.java
blob: 17c25b2e8e07e8ee7140ac87671df2e9bf1a9596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.onap.sdc.dcae.errormng;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;

public class BaseException extends HttpClientErrorException {

    private static final Gson gson = new Gson();

    protected final transient RequestError requestError;

    public BaseException(HttpClientErrorException theError) {
        super(theError.getStatusCode());
        String body = theError.getResponseBodyAsString();
        if (body != null) {
            requestError = extractRequestError(body);
        } else {
            requestError = null;
        }
    }

    public BaseException(HttpStatus status, RequestError re){
        super(status);
        requestError = re;
    }

    public RequestError getRequestError() {
        return requestError;
    }

    private RequestError extractRequestError(String error) {
        ResponseFormat responseFormat = gson.fromJson(error, ResponseFormat.class);
        return responseFormat.getRequestError();
    }

    @JsonIgnore
    public String getMessageId() {
        return requestError.getMessageId();
    }

    @JsonIgnore
    public String[] getVariables() {
        return requestError.getVariables();
    }

    @JsonIgnore
    public String getText(){
        return requestError.getText();
    }

    @Override
    @JsonIgnore
    public String getMessage() {
        return requestError.getFormattedMessage();
    }

}