-
Notifications
You must be signed in to change notification settings - Fork 0
/
EJBExceptionMapper.java
55 lines (47 loc) · 1.61 KB
/
EJBExceptionMapper.java
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
package com.example.payara.hello;
import jakarta.ejb.EJBException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.Providers;
/**
* Maps {@link EJBException} thrown by e.g. EJB services and tries to unwrap and rethrow the exception.
*/
@Provider
public class EJBExceptionMapper implements ExceptionMapper<EJBException> {
@Context
private Providers providers;
@Override
public Response toResponse(final EJBException exception) {
Throwable handledException = exception;
try {
unwrapEJBException(exception);
} catch (Throwable e) {
// Exception is unwrapped into something not EJBException related
// Try to get the mapper for it
handledException = e;
}
ExceptionMapper<Throwable> mapper = providers.getExceptionMapper((Class<Throwable>) handledException.getClass());
return mapper.toResponse(handledException);
}
/**
* Throws the first nested exception which isn't an {@link EJBException}.
*
* @param wrappingException the EJBException
*/
public static void unwrapEJBException(final EJBException wrappingException) throws Throwable {
Throwable pE = null;
Throwable cE = wrappingException;
while (cE != null && cE.getCause() != pE) {
if (!(cE instanceof EJBException)) {
throw cE;
}
pE = cE;
cE = cE.getCause();
}
if (cE != null) {
throw cE;
}
}
}