-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRestServiceExceptionFacade.java
More file actions
90 lines (69 loc) · 2.84 KB
/
Copy pathRestServiceExceptionFacade.java
File metadata and controls
90 lines (69 loc) · 2.84 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.devonfw.quarkus.general.service.exception;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
@Provider
@Slf4j
public class RestServiceExceptionFacade implements ExceptionMapper<RuntimeException> {
private boolean exposeInternalErrorDetails = false;
@Context
UriInfo uriInfo;
@Override
public Response toResponse(RuntimeException exception) {
log.error("Exception:{},URL:{},ERROR:{}", exception.getClass().getCanonicalName(), this.uriInfo.getRequestUri(),
exception.getMessage());
if (exception instanceof ApplicationBusinessException) {
return createResponse((ApplicationBusinessException) exception);
} else if (exception instanceof WebApplicationException) {
return createResponse((WebApplicationException) exception);
}
return createResponse(exception);
}
private Response createResponse(ApplicationBusinessException exception) {
int status = exception.getStatusCode() == null ? Status.BAD_REQUEST.getStatusCode() : exception.getStatusCode();
return createResponse(status, exception.getCode(), exception);
}
private Response createResponse(WebApplicationException exception) {
Status status = Status.fromStatusCode(exception.getResponse().getStatus());
return createResponse(status.getStatusCode(), exception.getClass().getSimpleName(), exception);
}
private Response createResponse(Exception exception) {
return createResponse(Status.INTERNAL_SERVER_ERROR.getStatusCode(), exception.getClass().getSimpleName(),
exception);
}
private Response createResponse(int status, String errorCode, Exception exception) {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("errorCode", errorCode);
if (this.exposeInternalErrorDetails) {
jsonMap.put("message", getExposedErrorDetails(exception));
} else {
jsonMap.put("message", exception.getMessage());
}
jsonMap.put("uri", this.uriInfo.getPath());
jsonMap.put("timestamp", ZonedDateTime.now().toString());
return Response.status(status).type(MediaType.APPLICATION_JSON).entity(jsonMap).build();
}
private String getExposedErrorDetails(Throwable error) {
StringBuilder buffer = new StringBuilder();
Throwable e = error;
while (e != null) {
if (buffer.length() > 0) {
buffer.append(System.lineSeparator());
}
buffer.append(e.getClass().getSimpleName());
buffer.append(": ");
buffer.append(e.getLocalizedMessage());
e = e.getCause();
}
return buffer.toString();
}
}