how to handle 500 error

4.8k views Asked by At

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

   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.

0

There are 0 answers