Expand my Community achievements bar.

SOLVED

Displaying friendly errors in components

Avatar

Level 1

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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. 

View solution in original post

4 Replies

Avatar

Level 2

<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. 

Avatar

Correct answer by
Level 10

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. 

Avatar

Level 7

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
 

Avatar

Level 1

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