Displaying friendly errors in components | Community
Skip to main content
October 16, 2015
Solved

Displaying friendly errors in components

  • October 16, 2015
  • 4 replies
  • 2121 views

Hi,

I want to be able to catch all exceptions that occur at the component level, and display a friendly error message within that component without affecting the rest of the page rendering.  What is the best way to do this?

Thanks in advance for your help.

Jason

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

CQ components are just JSP scripts. So you can display an error message in a DIV. Hide the DIV when you do not want to display the message. 

4 replies

Naidu_Jakkana
Level 2
October 16, 2015

<c:catch var ="catchException">

   <c:set var="test" value="object.title" />

</c:catch>

<c:if test = "${catchException != null}">

    <%@include file="/apps/sni-core/error.jsp" %>

</c:if>

 

 

In the error jsp we will display a message saying:

 

<%

Logger logger=Logger.getLogger(this.getClass().getName());

String errorMessage = pageContext.findAttribute("catchException");

logger.error("Exception Occurred in the module : " + errorMessage);

%>

There is an exception while rendering this module. then the exception occurred is : ${catchException}

we can even print the exception to a log file.

we just need to add "<c:catch var ="catchException">" in every module, so that we can handle the errors at module levels. 

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

CQ components are just JSP scripts. So you can display an error message in a DIV. Hide the DIV when you do not want to display the message. 

Ojjis
Level 7
October 16, 2015

As Scott pointed out, it's possible to handle the errors straight away when they arise on the JSP page.
There are a few examples here of how you could do it: http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm

If would maybe make use of the try/catch blocks so you can handle the errors nicely on the actual component jsp and you could do create some nice
html for the catch block so that the component would look good anyway. That might include a special DIV when an error arise but it would not display that part if everything went fine.

/Johan
 

October 16, 2015

Ojjis wrote...

As Scott pointed out, it's possible to handle the errors straight away when they arise on the JSP page.
There are a few examples here of how you could do it: http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm

If would maybe make use of the try/catch blocks so you can handle the errors nicely on the actual component jsp and you could do create some nice
html for the catch block so that the component would look good anyway. That might include a special DIV when an error arise but it would not display that part if everything went fine.

/Johan
 

 

Thank you for your replies, I appreciate it.  I was trying to get away from enforcing a rule that all developers be required to add a general try/catch block around all of the code for their individual components.  What I was looking for was some way to automatically add a try/catch block around any component dropped onto a page, but to have the code that does that in only one place.  Is there a way to do this?

Thanks!
Jason