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

Custom error handler not reflecting error code for 500

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

29-04-2019

Hi,

I've overridden the OOTB error handler and it works fine for 404 pages.

For 500 error, I've created a Throwable.jsp and inside the calling service class which internally set the response code 500 and include the page

RequestDispatcher dispatcher = request.getRequestDispatcher(path);

response.setStatus(500);

dispatcher.include(new GetRequest(request), response);

return;

Even though I'm setting the 500 error, the page returns 200. And, also, whatever be the content of error page, it's including inside loaded page which is like, it shows headers twice. I believe that 'cos of loading page already has header and another one is coming from error page which is included inside loading page.

Could you please check?

Best regards,
Himanshu Singhal

Replies

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

29-04-2019

Errata; It‘s "Response already committed"

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

29-04-2019

And, I think I understand the problem why it's not working for 500.
For the 404, since the resource doesn't exist, in that case, it doesn't make any call to any of AEM resource and hence directly jump
to error handler and render the error page (could be via include, forward or sendRedirect - all works in this case)

But, for 500, since the actual page resource exist but only let's say a component in page having some issue in backend and due to that it throws 500 error. But, before that, we have an authentication handler running which actually sets the anonymous token and cookie on the page and that could be the reason while I try to set 500 and try to do sendRedirect or forward, in that case, it shows "Response already committed".

With include, it does work 'cos in that case, it includes the error page resource inside actual rendering page and include doesn't change the status of already existing resource/page.

Kindly lemme know if the explanation makes more sense. And, considering the use case, what could be the implementation approach?

Avatar

Avatar
Establish
Level 6
antoniom5495929
Level 6

Likes

90 likes

Total Posts

212 posts

Correct Reply

39 solutions
Top badges earned
Establish
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Establish
Level 6
antoniom5495929
Level 6

Likes

90 likes

Total Posts

212 posts

Correct Reply

39 solutions
Top badges earned
Establish
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile
antoniom5495929
Level 6

30-04-2019

Hi,

I'm not sure regarding the explanation. Could you share with us the code that you are using in order to manage the error?

Let me know.

Thanks,

Antonio

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

30-04-2019

The code snippet is the same as I shared in question post. The problem is that when the component logic is invoked(the one which throws error), before that there are other components sling models are called and some of it are writing the JSON response back on page and also setting up the cookies.

Could it be the reason that it's not able to redirect and throws error "Response already committed"?

RequestDispatcher dispatcher = request.getRequestDispatcher(path); 

response.setStatus(500); 

dispatcher.forward(request, response); 

return;

Avatar

Avatar
Establish
Level 6
antoniom5495929
Level 6

Likes

90 likes

Total Posts

212 posts

Correct Reply

39 solutions
Top badges earned
Establish
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Establish
Level 6
antoniom5495929
Level 6

Likes

90 likes

Total Posts

212 posts

Correct Reply

39 solutions
Top badges earned
Establish
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile
antoniom5495929
Level 6

30-04-2019

Hi,

let me understand, your requirement is that if a 500 is returned during the rendering of your page (e.g. for a single component) all the html request is in status 500?

I think that doesn't make sense. In that way also for a simple error you avoid the rendering of all the page.

Otherwise please try to explain your requirement because I don't understand correctly which is the expected behaviour.

Thanks,

Antonio

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

30-04-2019

Let's say in a page I've 10 components and out of which one component backend logic doesn't work as expected and throws null pointer exception. In that case, it breaks the rendering of whole page yet the page status it displays is 200 which is not okay.

So, if there's any 500 error occurs due to (I messed up situation) then in that case we'd like to show the 500 page status and display error page.

Now, to handle the exceptions I've created a Throwable.jsp and in that, return the logic which calls a service, finds out the error page specific to error code then try to forwards the request to error page. When it does, it throws error "Response already committed".

That could be because there're other components on page and corresponding to components, sling models are called and those are writing data in response. So, since response already having some data, in that case, forwarding might not be possible.

That's the scenario.