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;

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

报告相同问题?

悬赏问题

  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。