Skip to content

Commit

Permalink
Better handle missing canonical value
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 18, 2022
1 parent 4a0afa4 commit e98d8e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Rgasch\Collection\Collection;
use RuntimeException;
use function str_starts_with;
Expand Down Expand Up @@ -77,11 +78,15 @@ protected function handleCreate(): void

protected function getCanonicalValue(PublicationFieldType $canonicalFieldDefinition, string $canonicalFieldName): string
{
if ($canonicalFieldDefinition->type === PublicationFieldTypes::Array) {
$canonicalValue = $this->fieldData->{$canonicalFieldName}[0];
} else {
$canonicalValue = $this->fieldData->{$canonicalFieldName};
try {
if ($canonicalFieldDefinition->type === PublicationFieldTypes::Array) {
$canonicalValue = $this->fieldData->{$canonicalFieldName}[0];
} else {
$canonicalValue = $this->fieldData->{$canonicalFieldName};
}
return $canonicalValue;
} catch (InvalidArgumentException $exception) {
throw new RuntimeException("Could not find field value for '$canonicalFieldName' which is required for as it's the type's canonical field", 404, $exception);
}
return $canonicalValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testCreateWithoutSupplyingCanonicalField()
]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not find field value for title which is required for this type as it\'s the canonical field');
$this->expectExceptionMessage("Could not find field value for 'title' which is required for as it's the type's canonical field");
$creator = new CreatesNewPublicationPage($pubType, $fieldData);
$creator->create();
}
Expand Down

0 comments on commit e98d8e1

Please sign in to comment.