views:

247

answers:

2

In certain error conditions I would like to redirect to the Home controller but I want to make sure that I don't get into a "redirect" loop (with an exception being generated each time resulting in yet another redirect). What is the best way to do this?

+1  A: 

Well, the general way to stop recursion is with a stop case. In your example, whatever does the redirect could look at its referrer, and make sure it does not try to "redirect" to the referrer.

Craig Stuntz
Actually, that is not what I want. Sometimes there is a condition where there is an exception but I want to redirect even if it's referred from the same URL, because the next time it will succeed. I just don't want to keep doing it forever.
JoelFan
I cannot in good conscience help out with that. It would be like giving a dinosaur directions to the La Brea Tar Pits.
Craig Stuntz
+1  A: 

It sounds like you're asking for a solution to the halting problem (i.e. "I want to redirect to self on exceptions but not if subsequent call will always throw an exception").

Barring implementing research in this field (while the halting problem is impossible to solve, you can eliminate quite a few cases with certain analysis) I would say your best bet is to do what firefox is doing anyway - keep a count of how many self-redirects have occurred and stop redirecting if it exceeds a certain count.

A way to do this might be to include a parameter that is incremented with each subsequent "exception throw redirect" and to compare against this parameter when deciding whether to redirect or not.

Graphain