Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

how to handle 500 error

Avatar

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile
Sam205505050
Level 4

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 1
Level 2
rakesh_kumar1
Level 2

Like

1 like

Total Posts

6 posts

Correct Reply

4 solutions
Top badges earned
Boost 1
Affirm 3
Affirm 1
View profile

Avatar
Boost 1
Level 2
rakesh_kumar1
Level 2

Like

1 like

Total Posts

6 posts

Correct Reply

4 solutions
Top badges earned
Boost 1
Affirm 3
Affirm 1
View profile
rakesh_kumar1
Level 2

15-10-2015

You are just setting the 500 status, which is not picked by your error handler(which is wrongly implemented of course) , actually the sling "default" error handler kicks in and sends the "Internal Server Error".

you have two options:

// 1.

500.jsp script can be created. However, it is only used if HttpServletResponse.sendError(500) is executed explicitly; i.e. from an exception catcher.

Otherwise, the response code is set to 500, but the 500.jsp script is not executed..

You are not doing this in your servlet code.

 

// 2.

Update you error servlet as given below.

 

/** * Generic Error Handler Servlet that handles all the uncaught exceptions thrown by application code. * * @author Rakesh.Kumar * */ @Component(immediate = true) @SlingServlet(description = "Generic Error Handler Servlet that handles all the uncaught exceptions thrown by application code.", resourceTypes = { "sling/servlet/errorhandler" }, extensions = { "Throwable" }, generateComponent = false) public class ErrorHandlerServlet extends SlingAllMethodsServlet { /** * serialVersionUID for this class. */ private static final long serialVersionUID = -7199024830200774412L; /** * Logger for this class */ private static final Logger LOGGER = LoggerFactory .getLogger(ErrorHandlerServlet.class); /** * Generic Error Handler Servlet that handles all the uncaught exceptions * thrown by application code * * @param request *            {@link SlingHttpServletRequest} * @param response *            {@link SlingHttpServletResponse} * * @throws ServletException * @throws IOException */ @Override protected void service(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { LOGGER.debug("Handling the uncaught exception."); Object exception = request.getAttribute(SlingConstants.ERROR_EXCEPTION); if (exception instanceof Exception) { Exception ex = (Exception) exception; String message = (String) request .getAttribute(SlingConstants.ERROR_MESSAGE); Integer statusCode = (Integer) request .getAttribute(SlingConstants.ERROR_STATUS); if (statusCode == null) { statusCode = Integer .valueOf(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); } // Logs the exception and other information. // forward/redirect your request to desired page and before that make sure response is not committed response.isCommitted } } }

Now for all uncaught exceptions(500) your Error handler will be called.

 

HTH.

~cheers

Rakesh

Answers (3)

Answers (3)

Avatar

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
hemantbellani-O
Level 2

16-12-2016

        Thanks Rakesh, a handy example for custom Error Handler Servlet

Avatar

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile

Avatar
Validate 25
Level 4
Sam205505050
Level 4

Likes

26 likes

Total Posts

200 posts

Correct Reply

30 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 3
Ignite 1
View profile
Sam205505050
Level 4

15-10-2015

overlay default.jsp under /apps/sling/servlet/errorhandler and added above code, after it was fine. also, I used response.sendError(500) for testing. Not sure why Exception.jsp/Throwable.jsp did not work

Avatar

Avatar
Validate 1
Level 2
Venugopal_Gumma
Level 2

Likes

0 likes

Total Posts

24 posts

Correct Reply

10 solutions
Top badges earned
Validate 1
Affirm 5
Affirm 3
Affirm 10
Affirm 1
View profile

Avatar
Validate 1
Level 2
Venugopal_Gumma
Level 2

Likes

0 likes

Total Posts

24 posts

Correct Reply

10 solutions
Top badges earned
Validate 1
Affirm 5
Affirm 3
Affirm 10
Affirm 1
View profile
Venugopal_Gumma
Level 2

15-10-2015