blob: 8ed68fa0322f8100c9b976c912cde0951252b471 (
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
|
package org.openecomp.sdc.be.servlets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.stereotype.Component;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Component
@Provider
public class ComponentExceptionMapper implements ExceptionMapper<ComponentException> {
private final ComponentsUtils componentsUtils;
protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
public ComponentExceptionMapper(ComponentsUtils componentsUtils) {
this.componentsUtils = componentsUtils;
}
@Override
public Response toResponse(ComponentException componentException) {
ResponseFormat responseFormat = componentException.getResponseFormat();
if (componentException.getResponseFormat()==null) {
responseFormat = componentsUtils.getResponseFormat(componentException.getActionStatus(), componentException.getParams());
}
return Response.status(responseFormat.getStatus())
.entity(gson.toJson(responseFormat.getRequestError()))
.build();
}
}
|