doufei2007 2016-11-14 07:59
浏览 146
已采纳

Laravel在数据库中报告错误

I'm making a request to URL to get data using Goutte. But the server where I'm making request is slow. So sometimes laravel throws error of time out. When this error comes, I have to make entry of this error log in databse with some additional data (i.e, id etc). I have searched on internet. But I found all solutions related to customise error message etc. What I want is when laravel throws error of time out, I have to make entry in database with additional data and then redirect page. If any one knows the solution, it will be appreciated.

Here is my code.

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]);
        }
    }
}

Here is my error message

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)

Also getting this error sometimes

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

I'm using laravel 5.3 and this scraper.

  • 写回答

1条回答 默认 最新

  • doulin8374 2016-11-14 08:24
    关注

    Well, this is how I would do it:

    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();
            }
        }
    }
    

    You can also move the saving of the log in a private method, I left it like this so you can see that it is possible to treat several exceptions differently, or you could catch as a general exception:

        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();
            }
        }
    

    If you want to log in some files you have the Log facade: use Illuminate\Support\Facades\Log;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答