Skip to content

Commit b84f9c2

Browse files
committed
Add explicit exception handling for missing association type codes
This commit addresses PR feedback to improve error handling by throwing an explicit exception instead of silently continuing when an association type code cannot be retrieved. Changes made: - Created ProductAssociationTypeNotFoundException with descriptive message including the association ID for better debugging - Updated ProductNormalizer to throw the exception instead of silently skipping associations with null type codes - Added unit test to verify the exception is thrown when type code is null This change improves debugging and makes error conditions explicit rather than silently failing, which is better for production systems where missing association type codes likely indicate a data integrity issue that should be addressed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0b35648 commit b84f9c2

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Api/Normalizer/ProductNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer;
66

77
use CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface;
8+
use CommerceWeavers\SyliusAlsoBoughtPlugin\Exception\ProductAssociationTypeNotFoundException;
89
use Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface;
910
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
1011
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
@@ -39,7 +40,7 @@ public function normalize($object, string $format = null, array $context = []):
3940
$associationTypeCode = $this->getAssociationTypeCodeByAssociationIdQuery->get((int) $id);
4041

4142
if (null === $associationTypeCode) {
42-
continue;
43+
throw new ProductAssociationTypeNotFoundException((int) $id);
4344
}
4445

4546
$object['associations'][$associationTypeCode] = $association;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Exception;
6+
7+
final class ProductAssociationTypeNotFoundException extends \RuntimeException
8+
{
9+
public function __construct(int $associationId)
10+
{
11+
parent::__construct(sprintf('Product association type not found for association with id "%d".', $associationId));
12+
}
13+
}

tests/Unit/Api/Normalizer/ProductNormalizerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer\ProductNormalizer;
88
use CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface;
9+
use CommerceWeavers\SyliusAlsoBoughtPlugin\Exception\ProductAssociationTypeNotFoundException;
910
use PHPUnit\Framework\TestCase;
1011
use Prophecy\PhpUnit\ProphecyTrait;
1112
use Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface;
@@ -54,6 +55,35 @@ public function testItAddsAssociationsTypesToProductResponse(): void
5455
$normalizer->normalize($product->reveal())
5556
);
5657
}
58+
59+
public function testItThrowsExceptionWhenAssociationTypeCodeIsNull(): void
60+
{
61+
$baseNormalizer = $this->prophesize(ProductNormalizerInterface::class);
62+
$query = $this->prophesize(GetAssociationTypeCodeByAssociationIdQueryInterface::class);
63+
$iriToIdentifierConverter = $this->prophesize(IriToIdentifierConverterInterface::class);
64+
65+
$normalizer = new ProductNormalizer(
66+
$baseNormalizer->reveal(),
67+
$query->reveal(),
68+
$iriToIdentifierConverter->reveal(),
69+
);
70+
71+
$product = $this->prophesize(Product::class);
72+
$baseNormalizer->normalize($product->reveal(), null, [])->willReturn([
73+
'code' => 'product_code',
74+
'associations' => [
75+
'/api/v2/product-associations/1',
76+
],
77+
]);
78+
79+
$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/1')->willReturn('1');
80+
$query->get(1)->willReturn(null);
81+
82+
$this->expectException(ProductAssociationTypeNotFoundException::class);
83+
$this->expectExceptionMessage('Product association type not found for association with id "1".');
84+
85+
$normalizer->normalize($product->reveal());
86+
}
5787
}
5888

5989
interface ProductNormalizerInterface extends ContextAwareNormalizerInterface, NormalizerAwareInterface

0 commit comments

Comments
 (0)