Catching Kohana_Request_Exception in Kohana v3 during execution -- unexpected behaviour
As detailed here: Need assistance with Kohana 3 and catch all route turning into a 404 error as the accepted answer to the question, I'm attempting to catch errors thrown by Kohana to display pretty error pages and send co开发者_开发知识库rrect HTTP codes.
Here's a simplified version to demonstrate the problem:
try {
// Instantiate your Request object
$request = Request::instance();
// The give it a try, to see if its a valid request
$request->execute();
}
catch (Kohana_Request_Exception $e) {
header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, 404);
echo Request::factory('err/404')->send_headers()->execute()->response;
exit;
}
echo $request->send_headers()->response;
So I navigate to a non-existent URL such as http://example.local/moo/ and I get the following response
Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI: moo
Here is what's happening-- The request is being tried, failing with a Kohana_Request_Exception, it's being caught BUT when I try to build a new request object, Request::factory('err/404')
THAT request throws the error from my first request....!? wtf??
I've fiddled with it for a good hour and am as puzzled as when I started. Shouldn't the new request from the factory have no knowledge of the old request?? Why is this code malfunctioning when I essentially copied it from the d00d's answer?
// Release version and codename
const VERSION = '3.0.7';
const CODENAME = 'hattrick';
Someone point me in the right direction.. thx guys.
It's because within Request::factory('err/404')
, the err/404
part is a URL that is trying to be matched by your routes as well. Will it ever match, i.e. do you have a route with something like this...
Route::set('errors', 'err/<action>', array('action' => '404|500')) {}
You should also be sending a 404 via...
$request->status = 404;
Aha! Thanks guys but I got it figured.. I shoulda looked at the stack trace a little closer, the error page's template controller's before()
method was throwing a new error as it was attempting to check authentication using Request::instance()
I changed it to Request::current()
and it's all shiny.
Thanks for the help!
精彩评论