From 0db25b506a8411a5e1644ebaa67123a6eb7b6a77 Mon Sep 17 00:00:00 2001 From: Jeroen van den Enden Date: Fri, 29 Sep 2023 14:03:20 +0000 Subject: [PATCH] Improve error messages --- src/ImageData/LabelImageData.php | 2 +- src/ImageData/LogoImageData.php | 12 +++++++++--- tests/QrCodeTest.php | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ImageData/LabelImageData.php b/src/ImageData/LabelImageData.php index 3ddc75d..7c02009 100644 --- a/src/ImageData/LabelImageData.php +++ b/src/ImageData/LabelImageData.php @@ -16,7 +16,7 @@ private function __construct( public static function createForLabel(LabelInterface $label): self { - if (false !== strpos($label->getText(), "\n")) { + if (str_contains($label->getText(), "\n")) { throw new \Exception('Label does not support line breaks'); } diff --git a/src/ImageData/LogoImageData.php b/src/ImageData/LogoImageData.php index d823cce..1abe6f1 100644 --- a/src/ImageData/LogoImageData.php +++ b/src/ImageData/LogoImageData.php @@ -20,10 +20,12 @@ private function __construct( public static function createForLogo(LogoInterface $logo): self { + error_clear_last(); $data = @file_get_contents($logo->getPath()); if (!is_string($data)) { - throw new \Exception(sprintf('Invalid data at path "%s"', $logo->getPath())); + $errorDetails = error_get_last()['message'] ?? 'invalid data'; + throw new \Exception(sprintf('Could not read logo image data from path "%s": %s', $logo->getPath(), $errorDetails)); } if (false !== filter_var($logo->getPath(), FILTER_VALIDATE_URL)) { @@ -43,10 +45,12 @@ public static function createForLogo(LogoInterface $logo): self return new self($data, null, $mimeType, $width, $height, $logo->getPunchoutBackground()); } + error_clear_last(); $image = @imagecreatefromstring($data); if (!$image) { - throw new \Exception(sprintf('Unable to parse image data at path "%s"', $logo->getPath())); + $errorDetails = error_get_last()['message'] ?? 'invalid data'; + throw new \Exception(sprintf('Unable to parse image data at path "%s": %s', $logo->getPath(), $errorDetails)); } // No target width and height specified: use from original image @@ -123,10 +127,12 @@ private static function detectMimeTypeFromPath(string $path): string throw new \Exception('You need the ext-fileinfo extension to determine logo mime type'); } + error_clear_last(); $mimeType = @mime_content_type($path); if (!is_string($mimeType)) { - throw new \Exception('Could not determine mime type'); + $errorDetails = error_get_last()['message'] ?? 'invalid data'; + throw new \Exception(sprintf('Could not determine mime type: %s', $errorDetails)); } if (!preg_match('#^image/#', $mimeType)) { diff --git a/tests/QrCodeTest.php b/tests/QrCodeTest.php index 19681ed..d2cd786 100644 --- a/tests/QrCodeTest.php +++ b/tests/QrCodeTest.php @@ -146,7 +146,7 @@ public function testInvalidLogoPath(): void $qrCode = QrCode::create('QR Code'); $logo = Logo::create('/my/invalid/path.png'); - $this->expectExceptionMessage('Invalid data at path "/my/invalid/path.png"'); + $this->expectExceptionMessageMatches('#Could not read logo image data from path "/my/invalid/path.png"#'); $writer->write($qrCode, $logo); }