Skip to content

Commit 6b06eff

Browse files
authored
[Bug] Capture curl errors when offline (alexjustesen#1826)
* capture curl errors when offline * removed duplicate options method
1 parent e260674 commit 6b06eff

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

app/Actions/GetExternalIpAddress.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,28 @@
22

33
namespace App\Actions;
44

5-
use Illuminate\Support\Facades\Cache;
65
use Illuminate\Support\Facades\Http;
76
use Illuminate\Support\Facades\Log;
87
use Illuminate\Support\Str;
98
use Lorisleiva\Actions\Concerns\AsAction;
9+
use Throwable;
1010

1111
class GetExternalIpAddress
1212
{
1313
use AsAction;
1414

1515
public function handle(): bool|string
1616
{
17-
$externalIp = Cache::remember('external_ip', 30, function (): bool|string {
17+
try {
1818
$response = Http::retry(3, 100)
19-
->get('https://icanhazip.com/');
19+
->timeout(5)
20+
->get(url: 'https://icanhazip.com/');
21+
} catch (Throwable $e) {
22+
Log::error('Failed to fetch external IP address.', [$e->getMessage()]);
2023

21-
if ($response->failed()) {
22-
$message = sprintf('Failed to fetch external IP address, %d', $response->status());
24+
return false;
25+
}
2326

24-
Log::warning($message);
25-
26-
return false;
27-
}
28-
29-
return Str::trim($response->body());
30-
});
31-
32-
return $externalIp;
27+
return Str::trim($response->body());
3328
}
3429
}

app/Actions/GetOoklaSpeedtestServers.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace App\Actions;
44

5-
use Illuminate\Http\Client\RequestException;
6-
use Illuminate\Http\Client\Response;
75
use Illuminate\Support\Facades\Http;
86
use Illuminate\Support\Facades\Log;
97
use Lorisleiva\Actions\Concerns\AsAction;
8+
use Throwable;
109

1110
class GetOoklaSpeedtestServers
1211
{
@@ -20,19 +19,19 @@ public function handle(): array
2019
'limit' => 20,
2120
];
2221

23-
$response = Http::retry(3, 250)
24-
->timeout(5)
25-
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query)
26-
->throw(function (Response $response, RequestException $e) {
27-
Log::error($e);
22+
try {
23+
$response = Http::retry(3, 250)
24+
->timeout(5)
25+
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query);
26+
} catch (Throwable $e) {
27+
Log::error('Unable to retrieve Ookla servers.', [$e->getMessage()]);
2828

29-
return [
30-
'0' => 'There was an issue retrieving Ookla speedtest servers, check the logs for more info.',
31-
];
32-
})
33-
->collect();
29+
return [
30+
'⚠️ Unable to retrieve Ookla servers, check internet connection and see logs.',
31+
];
32+
}
3433

35-
return $response->mapWithKeys(function (array $item, int $key) {
34+
return $response->collect()->mapWithKeys(function (array $item, int $key) {
3635
return [
3736
$item['id'] => $item['sponsor'].' ('.$item['name'].', '.$item['id'].')',
3837
];

app/Filament/Pages/Dashboard.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ protected function getHeaderActions(): array
5757
Forms\Components\Select::make('server_id')
5858
->label('Select Server')
5959
->helperText('Leave empty to run the speedtest without specifying a server.')
60-
->options(fn (): array => GetOoklaSpeedtestServers::run())
6160
->options(function (): array {
6261
return array_filter([
6362
'Manual servers' => Ookla::getConfigServers(),

0 commit comments

Comments
 (0)