Laravel - cURL exceception - try catch not working

712 Views Asked by At

It seems that the issue I am facing is straightforward but somehow the resolutions suggested here, here, and here are not working

I have got the following piece of code:

namespace App\Traits;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

...

try {

  $response = Http::timeout(3)->get('google.cim');

} catch (\Illuminate\Http\Client\ConnectionException $e) {

  Log::info('Connection refused #6 ');

}

I have already tried

  1. Using \Illuminate\Http\Client\ConnectionException $e
  2. Using \Exception $e
  3. Clearing cache php artisan optimize:clear

But I just see the exception in the logs while my custom message never appears in the log.

Thank you.

1

There are 1 best solutions below

1
On

Rather than catching a specific exception, try catching all exceptions using:

try {
    $response = Http::timeout(3)->get('google.cim');
} catch (\Exception $e) {
    Log::info($e->getMessage()); 
}

Also, check your config/logging.php file to make sure your logging level is set correctly. It should be debug or info:

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 14,
],

Don't forget to clear the config cache if you need to make change. Run: php artisan config:clear.