在执行过程中捕获Kohana v3中的Kohana_Request_Exception - 意外行为

时间:2022-10-13 12:35:44

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 correct HTTP codes.

详细信息如下:需要协助Kohana 3并将所有路由变成404错误作为问题的接受答案,我试图捕捉Kohana抛出的错误,以显示漂亮的错误页面并发送正确的HTTP代码。

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

所以我导航到一个不存在的URL,例如http://example.local/moo/,我得到以下响应

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??

这是正在发生的事情 - 请求正在尝试,失败的Kohana_Request_Exception,当我尝试构建一个新的请求对象,它被捕获但是,请求::工厂('错误/ 404')这个请求从我的第一个引发错误请求....!? 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?

我已经摆弄了好一个小时,就像我开始时一样困惑。不应该从工厂的新请求不知道旧的要求?当我从d00d的答案中复制它时,为什么这段代码出现故障?

// Release version and codename
const VERSION  = '3.0.7';
const CODENAME = 'hattrick';

Someone point me in the right direction.. thx guys.

有人指出我正确的方向.. thx伙计们。

2 个解决方案

#1


0  

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...

这是因为在Request :: factory('err / 404')中,err / 404部分是一个尝试与您的路由匹配的URL。会不会匹配,即你有这样的路线......

Route::set('errors', 'err/<action>', array('action' => '404|500')) {}

You should also be sending a 404 via...

你也应该发送404通过......

$request->status = 404;

#2


0  

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.

啊哈!谢谢大家,但我得到它想..我应该看看堆栈跟踪更近一点,错误页面的模板控制器的before()方法抛出一个新的错误,因为它试图使用Request :: instance()检查身份验证我改变了它对Request :: current()而且它都闪闪发亮。

Thanks for the help!

谢谢您的帮助!

#1


0  

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...

这是因为在Request :: factory('err / 404')中,err / 404部分是一个尝试与您的路由匹配的URL。会不会匹配,即你有这样的路线......

Route::set('errors', 'err/<action>', array('action' => '404|500')) {}

You should also be sending a 404 via...

你也应该发送404通过......

$request->status = 404;

#2


0  

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.

啊哈!谢谢大家,但我得到它想..我应该看看堆栈跟踪更近一点,错误页面的模板控制器的before()方法抛出一个新的错误,因为它试图使用Request :: instance()检查身份验证我改变了它对Request :: current()而且它都闪闪发亮。

Thanks for the help!

谢谢您的帮助!