Laravel 4 - 捕捉错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17132577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - Catch errors
提问by TheNiceGuy
It seems like its not possible to catch errors yourself, insted of getting the laravel 4 error output.
似乎不可能自己捕获错误,而是获得 laravel 4 错误输出。
For example if i try:
例如,如果我尝试:
$databaseConfig = Config::get('database.connections.mysql');
$connect = mysql_connect($databaseConfig['host'], $databaseConfig['username'], $databaseConfig['password']);
if (!$connect)
{
return 'error';
}
If a error occurs i won't get the "error", insted laravel shows me the exception (on that orange site).
如果发生错误,我不会得到“错误”,insted laravel 会向我显示异常(在那个橙色站点上)。
The same if you go ahead and try a
如果你继续尝试一个
try {
$pdo = DB::connection('mysql')->getPdo();
}
catch(PDOException $exception) {
return Response::make('Database error! ' . $exception->getCode());
}
Is there any way to do that?
有没有办法做到这一点?
回答by DerLola
The code you provided should work just fine. If I put this in my routes.php, I see the expected error string (without the orange).
您提供的代码应该可以正常工作。如果我把它放在我的 routes.php 中,我会看到预期的错误字符串(没有橙色)。
Route::get('error', function() {
try
{
$pdo = DB::connection('mysql')->getPdo();
}
catch(PDOException $exception)
{
return Response::make('Database error! ' . $exception->getCode());
}
return 'all fine';
});
What might be happening here, is that your PDOException isn't caught. Try added a backslash to the PDOException so you'll be sure it's the one defined in the root and not in the current namespace.
这里可能发生的情况是您的 PDOException 没有被捕获。尝试向 PDOException 添加一个反斜杠,这样您就可以确定它是在根中定义的,而不是在当前命名空间中定义的。
catch(\PDOException $exception)
Also, try to run the code directly from within the routes.php file and see if it behaves the same.
此外,尝试直接从 routes.php 文件中运行代码,看看它的行为是否相同。
回答by Andreas
Take a look at this page: http://laravel.com/docs/errors
看看这个页面:http: //laravel.com/docs/errors
Quick example:
快速示例:
App::error(function(PDOException $e)
{
Log::error($exception);
return Response::make('Database error! ' . $exception->getCode());
});
回答by arpit desai
App::error(function(Exception $exception) {
echo '<pre>';
echo 'MESSAGE :: ';
print_r($exception->getMessage());
echo '<br> CODE ::';
print_r($exception->getCode());
echo '<br> FILE NAME ::';
print_r($exception->getFile());
echo '<br> LINE NUMBER ::';
print_r($exception->getLine());
die();// if you want than only
});
put this code in your route file...
you will get ERROR MESSAGE with FILE NAME and ERROR LINE
all most errors will be covered.
将此代码放在您的路由文件中...
您将收到带有文件名和错误行的错误消息,
所有大多数错误都将被覆盖。
回答by Ngoc Nam
I know my answer is very late. But after referencing from above answers, I hadn't solved the problem.
我知道我的回答很晚了。但是参考上面的答案后,我还没有解决问题。
After looking at this site (very clear explanation here): https://stackify.com/php-try-catch-php-exception-tutorial/
看了这个网站后(这里解释得很清楚):https: //stackify.com/php-try-catch-php-exception-tutorial/
Here is an additional answer:
这是一个额外的答案:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);