Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Implemented Repository to REST Exception converters #63

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/bundle/EventListener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Ibexa\Bundle\Rest\EventListener;

use Ibexa\Contracts\Core\Repository\Exceptions\Exception as RepositoryException;
use Ibexa\Rest\Exception\Converter\RepositoryExceptionConverterInterface;
use Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand All @@ -31,12 +33,17 @@ class ResponseListener implements EventSubscriberInterface, LoggerAwareInterface
*/
private $viewDispatcher;

private RepositoryExceptionConverterInterface $repositoryExceptionConverter;

/**
* @param $viewDispatcher AcceptHeaderVisitorDispatcher
*/
public function __construct(AcceptHeaderVisitorDispatcher $viewDispatcher)
{
public function __construct(
AcceptHeaderVisitorDispatcher $viewDispatcher,
RepositoryExceptionConverterInterface $repositoryExceptionConverter
) {
$this->viewDispatcher = $viewDispatcher;
$this->repositoryExceptionConverter = $repositoryExceptionConverter;
}

/**
Expand Down Expand Up @@ -74,7 +81,7 @@ public function onKernelResultView(ViewEvent $event)
*
* @throws \Exception
*/
public function onKernelExceptionView(ExceptionEvent $event)
public function onKernelExceptionView(ExceptionEvent $event): void
{
if (!$event->getRequest()->attributes->get('is_rest_request')) {
return;
Expand All @@ -86,7 +93,9 @@ public function onKernelExceptionView(ExceptionEvent $event)
$event->setResponse(
$this->viewDispatcher->dispatch(
$event->getRequest(),
$exception
$exception instanceof RepositoryException
? $this->repositoryExceptionConverter->convert($exception)
: $exception
)
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/bundle/Resources/config/exception/converters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

_instanceof:
Ibexa\Rest\Exception\Converter\RepositoryExceptionConverterInterface:
tags: [ ibexa.rest.repository_exception.converter ]

Ibexa\Rest\Exception\Converter\ContentFieldValidationExceptionConverter: ~
11 changes: 11 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: exception/converters.yaml }

parameters:
ibexa.rest.output.visitor.json.regexps:
- '(^application/vnd\.ibexa\.api\.[A-Za-z]+\+json$)'
Expand Down Expand Up @@ -199,6 +202,7 @@ services:
Ibexa\Bundle\Rest\EventListener\ResponseListener:
arguments:
- '@Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher'
- '@Ibexa\Rest\Exception\Converter\RepositoryExceptionConverterInterface'
calls:
- ['setLogger', ['@?logger']]
tags:
Expand Down Expand Up @@ -369,3 +373,10 @@ services:
parent: hautelook.router.template
calls:
- [ setOption, [ strict_requirements, ~ ] ]

Ibexa\Rest\Exception\Converter\AggregateRepositoryExceptionConverter:
arguments:
$converters: !tagged_iterator ibexa.rest.repository_exception.converter

Ibexa\Rest\Exception\Converter\RepositoryExceptionConverterInterface:
alias: Ibexa\Rest\Exception\Converter\AggregateRepositoryExceptionConverter
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Exception\Converter;

use Ibexa\Contracts\Core\Repository\Exceptions\Exception as RepositoryException;
use Ibexa\Rest\Server\Output\ValueObjectVisitor\Exception;
use Throwable;

/**
* @internal
*/
final class AggregateRepositoryExceptionConverter implements RepositoryExceptionConverterInterface
{
/** @var iterable<RepositoryExceptionConverterInterface> */
private iterable $converters;

public function __construct(iterable $converters)
{
$this->converters = $converters;
}

public function convert(RepositoryException $exception): Throwable
{
foreach ($this->converters as $converter) {
if (!$converter->supports($exception)) {
continue;
}

return $converter->convert($exception);
}

return $exception;
}

public function supports(RepositoryException $exception): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Exception\Converter;

use Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException as APIContentFieldValidationException;
use Ibexa\Contracts\Core\Repository\Exceptions\Exception as RepositoryException;
use Ibexa\Rest\Server\Exceptions\ContentFieldValidationException;
use Throwable;

final class ContentFieldValidationExceptionConverter implements RepositoryExceptionConverterInterface
{
/**
* @param \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException $exception
*/
public function convert(RepositoryException $exception): Throwable
{
return new ContentFieldValidationException($exception);
}

public function supports(RepositoryException $exception): bool
{
return $exception instanceof APIContentFieldValidationException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Exception\Converter;

use Ibexa\Contracts\Core\Repository\Exceptions\Exception as RepositoryException;
use Throwable;

/**
* Convert Repository exceptions to their corresponding REST ValueObjectVisitor instances.
*
* @internal
*/
interface RepositoryExceptionConverterInterface
{
public function convert(RepositoryException $exception): Throwable;

public function supports(RepositoryException $exception): bool;
}