From e60e30f1997b8db6aa3e82b800d08d37cdfe65cf Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Thu, 28 Sep 2023 19:57:39 +0200 Subject: [PATCH 1/2] Use stream_socket_client() exception for better message --- src/Downloader.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Downloader.php b/src/Downloader.php index 8b227ac..6cda0bd 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -180,14 +180,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 $ex) { + $client = null; + $errorDescription = trim(str_replace('stream_socket_client():', '', $ex->getMessage())); + } if (! empty($errorDescription)) { throw $this->buildFailureException($connectTo, $errorDescription); From 0613c2f7fd63a3084b7664f61c4af14d4aaecc26 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Fri, 13 Oct 2023 03:17:12 +0200 Subject: [PATCH 2/2] catch exception better --- src/Downloader.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Downloader.php b/src/Downloader.php index 6cda0bd..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; @@ -190,9 +191,9 @@ protected function fetchCertificates(string $hostName): array $streamContext ); } - catch (\ErrorException $ex) { + catch (ErrorException $exception) { $client = null; - $errorDescription = trim(str_replace('stream_socket_client():', '', $ex->getMessage())); + $errorDescription = trim(str_replace('stream_socket_client():', '', $exception->getMessage())); } if (! empty($errorDescription)) {