[英]Laravel 4 - Catch errors
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). 如果发生错误,我将不会收到“错误”消息,插入的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? 有什么办法吗?
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. 这里可能发生的是没有捕获到您的PDOException。 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中添加一个反斜杠,以确保它是在根目录而不是在当前名称空间中定义的反斜杠。
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文件中运行代码,并查看其行为是否相同。
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());
});
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. 所有大多数错误都将被涵盖。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.