Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails - A previous call to redirect(..) has already redirected

Tags:

grails

In Grails app, occasionally seeing "Cannot issue redirect" in logs:

2011-04-27 12:18:40,469 [TP-Processor13] ERROR GrailsExceptionResolver - Cannot issue a redirect(..) here. A previous call to redirect(..) has already redirected the response. org.codehaus.groovy.grails.web.servlet.mvc.exceptions.CannotRedirectException: Cannot issue a redirect(..) here. A previous call to redirect(..) has already redirected the response. at com.coach.LoginController$_closure2.doCall(LoginController.groovy:90) ...

Not sure how to track this down. Any ideas or suggestions?

Todd

like image 776
Todd M Avatar asked Apr 27 '11 16:04

Todd M


2 Answers

Check the login controller; it seems like you are not returning from the action after redirect. E.g.:

if (some condition) {
    redirect ()
    return  // should return from here to finish the action, otherwise the rest of the code will be executed
}
like image 87
Sachin Anand Avatar answered Oct 17 '22 15:10

Sachin Anand


Although this question has been answered, I figured I'd share my experience for future trawlers. Hope it helps.

This happened to me because I wasn't doing a return after the redirect:

    if (test) {
        flash.message = "Error message."
        redirect(action: "list")
    }

    switch ( params.test ) {
        case "value": 
            redirect(action: "value", id: callInstance.id, version: callInstance.version)

After a redirect, Grails will keep going if there's no return. In my case, it hit the switch and went on to the second redirect, which is where the error arose. The code should look like the following:

    if (test) {
        flash.message = "Error message."
        redirect(action: "list")
        return
    }

    switch ( params.test ) {
        case "value": 
            redirect(action: "value", id: callInstance.id, version: callInstance.version)
            return

This code has been anonymised of course ;)

EDIT

Aw geeze. I just realized that this is Sachin's answer :/ Well, I'll leave it as an additional example.

like image 42
Charles Wood Avatar answered Oct 17 '22 15:10

Charles Wood