Anyone know why the following code failed to capture the exception?
package org.rythmengine.spring.web.servlet.view;
import org.rythmengine.RythmEngine;
import org.rythmengine.exception.RythmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
public class RythmExceptionHandler {
RythmEngine engine;
@Autowired
public RythmExceptionHandler(RythmConfigurer conf) {
this.engine = conf.getRythmEngine();
}
@ExceptionHandler(value = RythmException.class)
public ModelAndView defaultErrorHandler(RythmException e) throws Exception {
if (engine.mode().isProd()) {
throw e;
}
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.setViewName("errors/500.html");
return mav;
}
}
Found the issue. It needs to add one line in the configuration file:
And also need to add
@EnableWebMvc
annotation in additional to the@ControllerAdvice
annotation.However I can't force user to add the component in their configuration, or I want to make it transparent to user. Thus the solution become the following code:
This is not the end so far. It successfully capture the Exception in user's controller code, but not the exception in the view rendering process. I am looking for a way to add
HandlerInterceptor
so that it can handle error inDispatcherServlet.triggerAfterCompletion(...)
Updates
The above code proved not working. The final solution is adding the following annotation to an arbitrary class:
And yes, now I don't need user to add the
<context:component-scan ...>
into their xml configuration file.With regarding to render time exception handling, I cache the exception internally in the
RythmView
class in case there are error (usually compile error or parsing error) raised up atcheckResource(Locale)
call, and in the followingrenderMergedTemplateModel
call I will check if there are cached exception, if there is then render the exception screen, which is something like:And yes, the developer friendly screen feature is only available when you set devMode to true (which is by default false) for
RythmConfigurer
: