Akshay Vaghasiya
Akshay Vaghasiya

Reputation: 1657

Laravel error reporting in database

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.

Upvotes: 0

Views: 1357

Answers (1)

ka_lin
ka_lin

Reputation: 9442

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;

Upvotes: 1

Related Questions