Skip to content

Commit

Permalink
Handle errors when value object cannot be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Aug 3, 2023
1 parent 1356989 commit e53998e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bundle/Resources/config/services/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
class: Netgen\Layouts\Ez\Parameters\ValueObjectProvider\LocationProvider
arguments:
- "@ezpublish.api.repository"
- "@netgen_layouts.error.handler"

netgen_layouts.ezplatform.parameters.parameter_type.content:
class: Netgen\Layouts\Ez\Parameters\ParameterType\ContentType
Expand All @@ -24,6 +25,7 @@ services:
class: Netgen\Layouts\Ez\Parameters\ValueObjectProvider\ContentProvider
arguments:
- "@ezpublish.api.repository"
- "@netgen_layouts.error.handler"

netgen_layouts.ezplatform.parameters.parameter_type.content_type:
class: Netgen\Layouts\Ez\Parameters\ParameterType\ContentTypeType
Expand Down
8 changes: 7 additions & 1 deletion lib/Parameters/ValueObjectProvider/ContentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Content;
use Netgen\Layouts\Error\ErrorHandlerInterface;
use Netgen\Layouts\Parameters\ValueObjectProviderInterface;

final class ContentProvider implements ValueObjectProviderInterface
{
private Repository $repository;

public function __construct(Repository $repository)
private ErrorHandlerInterface $errorHandler;

public function __construct(Repository $repository, ErrorHandlerInterface $errorHandler)
{
$this->repository = $repository;
$this->errorHandler = $errorHandler;
}

public function getValueObject($value): ?Content
Expand All @@ -28,6 +32,8 @@ public function getValueObject($value): ?Content

return $content->contentInfo->mainLocationId !== null ? $content : null;
} catch (NotFoundException $e) {
$this->errorHandler->handleError($e);

return null;
}
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Parameters/ValueObjectProvider/LocationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Location;
use Netgen\Layouts\Error\ErrorHandlerInterface;
use Netgen\Layouts\Parameters\ValueObjectProviderInterface;

final class LocationProvider implements ValueObjectProviderInterface
{
private Repository $repository;

public function __construct(Repository $repository)
private ErrorHandlerInterface $errorHandler;

public function __construct(Repository $repository, ErrorHandlerInterface $errorHandler)
{
$this->repository = $repository;
$this->errorHandler = $errorHandler;
}

public function getValueObject($value): ?Location
Expand All @@ -25,6 +29,8 @@ public function getValueObject($value): ?Location
static fn (Repository $repository): Location => $repository->getLocationService()->loadLocation((int) $value),
);
} catch (NotFoundException $e) {
$this->errorHandler->handleError($e);

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
use Netgen\Layouts\Error\ErrorHandlerInterface;
use Netgen\Layouts\Ez\Parameters\ValueObjectProvider\ContentProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -40,6 +41,7 @@ protected function setUp(): void

$this->valueObjectProvider = new ContentProvider(
$this->repositoryMock,
$this->createMock(ErrorHandlerInterface::class),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Repository\Values\Content\Location;
use Netgen\Layouts\Error\ErrorHandlerInterface;
use Netgen\Layouts\Ez\Parameters\ValueObjectProvider\LocationProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -38,6 +39,7 @@ protected function setUp(): void

$this->valueObjectProvider = new LocationProvider(
$this->repositoryMock,
$this->createMock(ErrorHandlerInterface::class),
);
}

Expand Down

0 comments on commit e53998e

Please sign in to comment.