【问题标题】:Laravel error reporting in database数据库中的 Laravel 错误报告
【发布时间】:2017-03-27 19:41:30
【问题描述】:

我正在向 URL 发出请求以使用 Goutte 获取数据。但是我发出请求的服务器很慢。所以有时 laravel 会抛出超时错误。当这个错误发生时,我必须在数据库中输入这个错误日志,并带有一些额外的数据(即 id 等)。我在互联网上搜索过。但是我找到了所有与自定义错误消息等相关的解决方案。我想要的是当 laravel 抛出超时错误时,我必须在数据库中输入额外的数据,然后重定向页面。如果有人知道解决方案,将不胜感激。

这是我的代码。

use Goutte\Client;
class WebScrapingController extends Controller {
    public function index() {
        try {
            $this->crawler = $this->client->request('GET', $url . '?' . $data);
        }catch(Exception $e){
            // Here I want to make entry in database and then redirect to another page
            dd(['Connection time out', $i, $e]);
        }
    }
}

这是我的错误信息

ConnectException in CurlFactory.php line 186:
cURL error 7: Failed to connect to myurl port 80: Connection timed out (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

有时也会出现此错误

RequestException in CurlFactory.php line 187:
cURL error 56: Recv failure: Connection timed out (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

我正在使用 laravel 5.3 和 this scraper。

【问题讨论】:

    标签: php laravel-5 error-handling laravel-5.3 error-reporting


    【解决方案1】:

    好吧,这就是我的做法:

    use Goutte\Client;
    class WebScrapingController extends Controller {
        public function index() {
            try {
                $this->crawler = $this->client->request('GET', $url . '?' . $data);
            } catch(\ConnectException $e){
                $log = new Log();//define this as a model
                $log->setMessage($e->getMessage());
                $log->save();
            } catch(\RequestException $e){
                $log = new Log();//define this as a model
                $log->setMessage($e->getMessage());
                $log->save();
            } finally {
              $yourModel = YourNamespace\YourModel::find($url);//or, depends on your model structure and DB
              $yourModel = YourNamespace\YourModel::where('url',$url)->first();
            }
        }
    }
    

    您还可以将日志的saving 移动到私有方法中,我将其保留为这样,您可以看到可以不同地处理多个异常,或者您可以将其捕获为一般异常:

        public function index() {
            try {
                $this->crawler = $this->client->request('GET', $url . '?' . $data);
            } catch(\Exception $e){
                $log = new Log();//define this as a model
                $log->setMessage($e->getMessage());
                $log->save();
            } finally {
              $yourModel = YourNamespace\YourModel::find($url);//or, depends on your model structure and DB
              $yourModel = YourNamespace\YourModel::where('url',$url)->first();
            }
        }
    

    如果你想登录一些文件,你有Log门面:use Illuminate\Support\Facades\Log;

    【讨论】:

    • 答案如我所料。所以我很容易理解。但我无法理解finally的用法。你能解释一下吗?
    • 无论如何最后都会被执行,所以即使你记录了异常,你也可以查询数据库并检索和输出数据(见:php.net/manual/en/language.exceptions.php
    猜你喜欢
    • 2013-08-05
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2016-04-02
    • 2017-07-27
    相关资源
    最近更新 更多