My question is simple. Should we or should we not Handle/Catch Exceptions in Sling Models/WCMUsePojos ?
Details:
We have several SlingModels which are calling OSGi service methods, when any exception is thrown we are catching it right upto the SlingModel and then we are doing in the model @PostConstruct method
slingHttpResponse.sendError(500);
This doesn't seem to work for us, the response status is 500(checked in network tab of the browser) but the page loads anyways instead of loading our 500.jsp page or "Internal Server Error Page" that is setup.
Infact what has worked for us is re-throwing the exception to the default handler. This successfully loads the 500.jsp page.
Ex.
@PostConstruct // THIS WORKS
public void init() throws Exception{
try{
// Business Logic calling Injected OSGi Service Methods
}
catch(Exception e){
// Log exception and rethrow
LOG.error("Exception in Model",e);
throw e;
}
}
Is the above implementation Ideal? This works for as opposed to below code which DOES NOT WORK FOR US
@PostConstruct // THIS DOES NOT WORK PROPERLY
public void init() throws Exception{
try{
// Business Logic calling Injected OSGi Service Methods
}
catch(Exception e){
// Log exception and SEND ERROR
LOG.error("Exception in Model",e);
slingHttpResponse.sendError(500);
}
}