Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Sep 29, 2023
1 parent a122b85 commit 0db25b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ImageData/LabelImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
12 changes: 9 additions & 3 deletions src/ImageData/LogoImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 0db25b5

Please sign in to comment.