Exception Handling changed between wildfly 24 and 26.

168 views
Skip to first unread message

Elia Zaides

unread,
May 4, 2023, 4:15:48 PM5/4/23
to WildFly



Our current wildfly 24 application has EJB that are being exposed through resteasy .

We have a project, rest-beans , that all the rest exposed beans are defined there:

rest-beans.war
 | ---- WEB-INF/web.xml
 | ---- classes

inside the web.xml we define the beans that are exposed for rest:
<context-param>
<!-- JNDI locations of EJBs that are acting as Rest resources and sitting
in this war file -->
<param-name>resteasy.jndi.resources</param-name>
<param-value>java:module/MyEJBResource1, java:module/MyEJBResource2
</param-value>
</context-param>



Additionally we define an ExceptionMapper , that catches will map exceptions.


In MyEJBResource1 we have a following code:


@EJB InternalEJB  internalEJB;

public Response getMyData( UserId userId ) {
   
   // Get data from internal EJB
   try {
    InternalData internalData = internalEJB.getInternalData(userId);
   } catch (MyNotFoundException e) {
   
     throw new MySpecialRestException(e, my_context, userId);
   }
}



The logic inside getInternalData is something like:

public InternalData getInternalData(UserId userId) {
   if (!userService.UserExists(userId)) {
        throw new MyNotFoundException("UserId " + userId + " Not Found");
   }
   /// rest of the logic, not relevant to the question.
  }


In wildfly 24 the exception mapper is being invoked only after MySpecialRestException is thrown. While in wildfly 26 the exception mapper is being invoked when MyNotFoundException is being thrown, which is totally unexpected. Additionally the exception is being wrapped up in the following way:

EJBException
 |-- UndeclaredThrowableException
      | -- PrivilegedActionException
            | -- MyNotFoundException

How is it possible to revert back to the previous behaviour that the ExceptionMapper is being invoked only on exception that are coming from the EJBs that are exposed to rest endpoint, i.e. only the EJBs that have an actual interaction with the customer.

Thanks a lot, Elia



Bartosz Baranowski

unread,
May 8, 2023, 2:34:00 AM5/8/23
to WildFly
Hey, could you please provide a bit more detail about classes and setup?
Is there ExceptionMapper defined for MyNotFoundException ?

James Perkins

unread,
May 10, 2023, 8:44:29 PM5/10/23
to WildFly
If the exception, MyNotFoundException, is being invoked from a REST endpoint, it would be caught by an ExceptionMapper. Does the call to internalEJB.getInternalData(userId); also invoke a REST endpoint?

Elia Zaides

unread,
May 13, 2023, 1:38:13 AM5/13/23
to WildFly
Hi James,

Thanks for looking into this.

There is no other REST endoint, the internal EJB is pretty simple , get data from cache if not get data from database. Another thing is that the mapper is catching MyNotFoundException, which is invoked by the internalEJB. 

James Perkins

unread,
May 15, 2023, 12:50:13 PM5/15/23
to WildFly
Are you not referring to a Jakarta REST jakarta.ws.rs.ext.ExceptionMapper?

Elia Zaides

unread,
May 15, 2023, 12:59:18 PM5/15/23
to WildFly
Our code imports import javax.ws.rs.ext.ExceptionMapper; 

and then implements the interface:

public class ApplicationExceptionMapper implements ExceptionMapper<ApplicationException> {

@Override
public Response toResponse(ApplicationException exception) {
}

James Perkins

unread,
May 15, 2023, 3:24:41 PM5/15/23
to WildFly
I'm a little confused then. If you're not hitting a REST end point, then how would this ExceptionMapper be invoked?

Elia Zaides

unread,
May 15, 2023, 4:53:50 PM5/15/23
to WildFly
I think that is exactly what is also working not as my expectation, and that is exactly my question.

I would never expect that MyNotFoundException, that is being thrown from internalEJB, and is not being accessed through rest, would ever be caught in ExceptionMapper, and that is the behaviour in Wildfly 24, after migrating the project to wildfly 26, the mapper started to catch those exceptions. My expectation would be that only exceptions being thrown from MyEJBResource would be caught by the exception mapper, the same behaviour we currently have in wildfly 24.

James Perkins

unread,
May 15, 2023, 4:59:11 PM5/15/23
to WildFly
On Monday, May 15, 2023 at 1:53:50 PM UTC-7 Elia Zaides wrote:
I think that is exactly what is also working not as my expectation, and that is exactly my question.

I would never expect that MyNotFoundException, that is being thrown from internalEJB, and is not being accessed through rest, would ever be caught in ExceptionMapper, and that is the behaviour in Wildfly 24, after migrating the project to wildfly 26, the mapper started to catch those exceptions. My expectation would be that only exceptions being thrown from MyEJBResource would be caught by the exception mapper, the same behaviour we currently have in wildfly 24.

This reads a bit confusing to me. In short, exceptions thrown in an EJB should not be caught by an ExceptionMapper unless the EJB is being invoked through a REST endpoint or the EJB is a REST endpoint. REST, in WildFly's case RESTEasy, should not be invoked in this call stack at all.

If you have a reproducer it would be easier to understand I think. However, I realize that is not always possible to easily create.
Reply all
Reply to author
Forward
0 new messages