From ddb3fa174bf5b03cb0e6e2b16f346395790ecd4b Mon Sep 17 00:00:00 2001 From: github-actions Date: Sat, 28 Oct 2023 15:52:47 +0000 Subject: [PATCH] Merge pull request #1409 from hydephp/improve-featured-image-factory-exception-message Update the featured image factory to specify the file causing an exception https://github.com/hydephp/develop/commit/3980228604792aef7eee7022afea2254f0dbd809 --- src/Framework/Factories/BlogPostDataFactory.php | 5 ++++- src/Framework/Factories/FeaturedImageFactory.php | 9 ++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Framework/Factories/BlogPostDataFactory.php b/src/Framework/Factories/BlogPostDataFactory.php index a8dbfc59..8eb80a3d 100644 --- a/src/Framework/Factories/BlogPostDataFactory.php +++ b/src/Framework/Factories/BlogPostDataFactory.php @@ -41,10 +41,13 @@ class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSc protected readonly ?PostAuthor $author; protected readonly ?FeaturedImage $image; + private readonly string $filePath; + public function __construct(CoreDataObject $pageData) { $this->matter = $pageData->matter; $this->markdown = $pageData->markdown; + $this->filePath = $pageData->sourcePath; $this->description = $this->makeDescription(); $this->category = $this->makeCategory(); @@ -98,7 +101,7 @@ protected function makeAuthor(): ?PostAuthor protected function makeImage(): ?FeaturedImage { if ($this->getMatter('image')) { - return FeaturedImageFactory::make($this->matter); + return FeaturedImageFactory::make($this->matter, $this->filePath); } return null; diff --git a/src/Framework/Factories/FeaturedImageFactory.php b/src/Framework/Factories/FeaturedImageFactory.php index 5501edf8..a844ae1c 100644 --- a/src/Framework/Factories/FeaturedImageFactory.php +++ b/src/Framework/Factories/FeaturedImageFactory.php @@ -30,6 +30,7 @@ class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedI public function __construct( private readonly FrontMatter $matter, + private readonly ?string $filePath = null, ) { $this->source = $this->makeSource(); $this->altText = $this->getStringMatter('image.altText'); @@ -58,9 +59,9 @@ public function toArray(): array ]; } - public static function make(FrontMatter $matter): FeaturedImage + public static function make(FrontMatter $matter, ?string $filePath = null): FeaturedImage { - return new FeaturedImage(...(new static($matter))->toArray()); + return new FeaturedImage(...(new static($matter, $filePath))->toArray()); } protected function makeSource(): string @@ -68,9 +69,7 @@ protected function makeSource(): string $value = $this->getStringMatter('image') ?? $this->getStringMatter('image.source'); if (empty($value)) { - // Todo, we might want to add a note about which file caused the error. - // We could also check for these before calling the factory, and just ignore the image if it's not valid. - throw new RuntimeException('No featured image source was found'); + throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file')); } if (FeaturedImage::isRemote($value)) {