From 3d6c8761aa84f9d05c42d212dfc9ec5adb3dae40 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx <168024+rudiedirkx@users.noreply.github.com> Date: Tue, 17 Oct 2023 17:56:25 +0200 Subject: [PATCH] Use stream_socket_client() exception for better message (#183) * Use stream_socket_client() exception for better message * catch exception better --- src/Downloader.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Downloader.php b/src/Downloader.php index 8b227ac..daff508 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -2,6 +2,7 @@ namespace Spatie\SslCertificate; +use ErrorException; use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate; use Spatie\SslCertificate\Exceptions\InvalidIpAddress; @@ -180,14 +181,20 @@ protected function fetchCertificates(string $hostName): array $connectTo = $hostName; } - $client = @stream_socket_client( - "ssl://{$connectTo}:{$this->port}", - $errorNumber, - $errorDescription, - $this->timeout, - STREAM_CLIENT_CONNECT, - $streamContext - ); + try { + $client = stream_socket_client( + "ssl://{$connectTo}:{$this->port}", + $errorNumber, + $errorDescription, + $this->timeout, + STREAM_CLIENT_CONNECT, + $streamContext + ); + } + catch (ErrorException $exception) { + $client = null; + $errorDescription = trim(str_replace('stream_socket_client():', '', $exception->getMessage())); + } if (! empty($errorDescription)) { throw $this->buildFailureException($connectTo, $errorDescription);