4

New in KO 3.3 is the HTTP::redirect method, which works by throwing an HTTP_Exception_302, which bubbles up and gets handled by the system to do the actual redirect.

My question is: how can I do a redirect without catching its exception if I'm calling the redirect within a try...catch block?

e.g.:

try {
    if($var === TRUE){
        HTTP::redirect(URL::site($_REQUEST['redirect_uri']));
    }else{
        throw new Exception('Error');
    }
} catch(Exception $e) {
    $this->template->errors[] = $e->getMessage();
}

This will not cause a redirect, because the generic Exception handler will catch it. How do I avoid this?

Murray Rowan
  • 3,597
  • 2
  • 19
  • 32

2 Answers2

3
try {
    if($var === TRUE){
        HTTP::redirect(URL::site($_REQUEST['redirect_uri']));
    }else{
        throw new Exception('Error');
    }
} 
catch(HTTP_Exception_Redirect $e) {
    // just rethrow it
    throw $e;
}
catch(Exception $e) {
    $this->template->errors[] = $e->getMessage();
}
biakaveron
  • 5,493
  • 1
  • 16
  • 20
1

Don't be so liberal in your catching of exceptions. Catch what you expect, and nothing else. This problem shouldn't exist.

zombor
  • 3,247
  • 17
  • 30
  • Correct me if I'm wrong but that's exactly what I'm doing in this case? – Murray Rowan Apr 26 '13 at 19:45
  • No, you are being liberal in this: `catch(Exception $e)`. You have a contrived example where you are throwing an `Exception`. Throw a less generic exception. – zombor Apr 26 '13 at 20:26