How VRaptor3 handle Exception in Controller Methods

3 respostas
H

Hi, All

I have this situation,
in my controller layer, they may throw exceptions, and I want to handle them in one place, for example, I want to log the exception details and forward the request to a jsp page to display some info to our clients.

is there any ways to satisfy my request?

pls be kindly reply this top in English or Chinese~thanks

3 Respostas

H

you can create an interceptor to handle exceptions

http://guj.com.br/posts/list/136307.java

G

Hi haiker. You are using the Vraptor3 or other version?

Do you need handle exceptions to display a more friendly error page for users or only for logging?

There are a issue at github (http://github.com/caelum/vraptor/issues/#issue/158) to create a pretty exception handler. See this page too: http://guj.com.br/posts/list/30/136307.java

So if you only need to logging the error and return to a generic page, you can use the error-page into your web.xml and create a simple interceptor to generate your log. See bellow.

@Intercepts
@RequestScoped
public class MyLoggingInterceptor implements Interceptor {

    @Override
    public boolean accepts(ResourceMethod method) {
        return true; // intercept all requests
    }

    @Override
    public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
        throws InterceptionException {
         
        try {
            stack.next(method, resourceInstance);
        } catch (Exception e) {
            // my log code
            // you can only print the log message or send by email, etc
        }
    }
}

Best regards

H

thank you all for your kindly response.

I like vraptor, it makes things easy~ :stuck_out_tongue:

Criado 7 de dezembro de 2009
Ultima resposta 8 de dez. de 2009
Respostas 3
Participantes 3