Foren

Display an error message to the user from BaseModelListener subclass.

Sean Megeath, geändert vor 7 Jahren.

Display an error message to the user from BaseModelListener subclass.

New Member Beiträge: 14 Beitrittsdatum: 23.03.15 Neueste Beiträge
When any user is added or removed as a member of a site, I make a REST call to a non-Liferay site, in the class I have that extends BaseModelListener<User>. If that site is unavailable or the REST call fails for other reasons, I capture the exception and log an error message using slf4j. Now, I'm required to display a message to the user on the Liferay page (edit_site_assignments_users.jsp) and roll back the change on Liferay. It doesn't appear as though I can use SessionErrors to display to a liferay-ui:error tag, since my BaseModelListener<User> doesn't get a HttpServletRequest, HttpSession, PortletRequest, or PortletSession. Any suggestions on what I should do to present the problem to the user? Will I need to abandon using a model listener for this?
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Display an error message to the user from BaseModelListener subclass.

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Throw an exception. It will trigger the rollback.

If your exception extends PortalException, the MVCPortlet the login portlet extends will inject this exception into SessionErrors automagically.

Therefore the only other thing you need is a JSP fragment to override the JSP to add the <liferay-ui:error /> to display the appropriate message.
Sean Megeath, geändert vor 7 Jahren.

RE: Display an error message to the user from BaseModelListener subclass.

New Member Beiträge: 14 Beitrittsdatum: 23.03.15 Neueste Beiträge
Thanks for the reply! Here's what I interpreted your response to mean:
I created a class that extended PortalException, and gave that as the "cause" to the ModelListenerException I throw in the onAfterAddAssociation of my class that extends BaseModelListener<User>. In the webapps\ROOT\html\portlet\sites_admin\edit_site_assignments_users.jsp, I added a page import tag for my class that extends PortalException, then added a liferay-ui:error tag:
<liferay-ui:error exception="<%= ExtendedPortalException.class %>" message="Problem adding user to site." />
When I went to add a user to a site, I just got an error message drawn where the site membership interface was, saying "Site Memberships is temporarily unavailable." Which is what I was getting when I was just throwing the ModelListenerException and not having the liferay-ui:error tag. I'm wondering if I'm missing something basic here. Does anything stand out to you as a problem?
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Display an error message to the user from BaseModelListener subclass.

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Snap I wasn't thinking and was looking at login portlet, but I'm thinking the same rules should apply for the site memberships since it is also based on the Liferay MVCPorlet implementation.

You may need to create a command action override for the action servicing the request, it may need to dig into the causes to find if yours is in there and throw it manually.