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

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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档