Expand my Community achievements bar.

SOLVED

Sling Model 404 Status with 404.jsp

Avatar

Level 7

Hello,

I am trying to get my SlingModel to return status of 404... which is possible, but then I would also like my sling model to push the user to the/libs/sling/servlet/errorhandler/404.jsp page which I am expecting:

Cannot serve request to /content/my-site.home in BundledScriptServlet (/libs/sling/servlet/errorhandler/404.jsp)

 

How can I do this?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You are supposed to guarantee that your models won't throw any error of that type. There are a lot of things you can do to achieve such as:
- Use @optional annotations,

- validate null pointer exceptions

- use try/catch blocks,

- and most important write JUnit test



Esteban Bustamante

View solution in original post

5 Replies

Avatar

Community Advisor

The Sling Model by definition is not meant to be accessed by a user but for an OSGI component (1), so conceptually it is wrong to set 400 status a slingModel and the use case you are trying to achieve it is not correct conceptually.

If you need to achieve such a thing use a Servlet instead, there, you can set the status (as shown below) and handle easily the error page. 

 

response.setStatus(400)

 

 

For more info about how to handle error pages, you can read this: https://blog.3sharecorp.com/error-handling-in-aem 

(1) https://sling.apache.org/documentation/bundles/models.html 



Esteban Bustamante

Avatar

Level 7

Thank you, but still it would not work as I am trying to get the .html page to show up as 
Cannot serve request to /content/my-site.home in BundledScriptServlet (/libs/sling/servlet/errorhandler/404.jsp)

Avatar

Community Advisor

Why would you do that? That's not realistic, that error means that your model is not available and since this is meant to be used for you (not end-user) you should fix that, either you don't invoke the model or you add at least an empty model to avoid the issue.



Esteban Bustamante

Avatar

Level 7

For example, the page is requesting for ?currentPage... if this param does not exists, then we would like to show the 404 page immediately. 

Avatar

Correct answer by
Community Advisor

You are supposed to guarantee that your models won't throw any error of that type. There are a lot of things you can do to achieve such as:
- Use @optional annotations,

- validate null pointer exceptions

- use try/catch blocks,

- and most important write JUnit test



Esteban Bustamante