I have a sightly java class(a java class included inside component) which calls an api and checks whether the response in null. If its null, I have to call the error component. When I am setting the response status as 404, it is not working because before executing that line, it is taking the response as 200. How to call the error component in this case. Note: I have tried redirecting the URL to error.html, but thats not the proper solution.
1 Answers
0
votes
It is generally bad practice to redirect or change the response status from a component/view as the response might already have some content that was committed.
You might want to redesign you application to either:
- Call the remote API at the beginning of the request handling and, if the remote API returns null, return a 404 before anything else is committed to the response.
- Handle the null response from the remote API by either:
- Rendering an appropriate response, in case the null response is expected/permitted in some cases.
- Throwing an exception, which will result in status 500, in case the null response is not expected.