how to handle 500 error

Avatar

Avatar

Sam205505050

Avatar

Sam205505050

Sam205505050

15-10-2015

I'm trying to show custom error message for 404/403/500 error. I'm able to show 404/403 error but unable to show 500 error. Steps I have followed so far - Step 1: Created a servlet which will set the status as 500 for testing purpose 

public class ErrorServlet extends SlingSafeMethodsServlet {

@Override protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException { try{

response.setStatus(500);

} catch (Exception e) { e.printStackTrace(); } } }

Step2: Crated Exception.jsp and Throwable.jsp under apps/sling/servlet/errorhandler

Step3: Added below code in both Exception.jsp and Throwable.jsp

<%@include file="/libs/foundation/global.jsp"%> <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %><% %><sling:defineObjects /><% response.setStatus(500); request.getRequestDispatcher("/content/myproject/en_US/ errorpages/errorpage500.html").forward(request,response); %>

Now when I hit http://port:host/path/to/servlet then I get "Internal Server Error" in the browser. It does not send to the expected custom error page which I mentioned. Kindly let me know what I'm missing here.

View Entire Topic

Avatar

Avatar

hemantbellani-O

Avatar

hemantbellani-O

hemantbellani-O

16-12-2016

        Thanks Rakesh, a handy example for custom Error Handler Servlet