Skip to content
Open
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: 16 additions & 1 deletion src/State/Tests/ErrorProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@
namespace ApiPlatform\State\Tests;

use ApiPlatform\Metadata\Get;
use ApiPlatform\State\ApiResource\Error;
use ApiPlatform\State\ErrorProvider;
use ApiPlatform\Validator\Exception\ValidationException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;

class ErrorProviderTest extends TestCase
{
public function testCreateFromExceptionWithValidationException(): void
{
$violation = new ConstraintViolation('This value is too long.', null, [], null, 'name', 'toolong');
$exception = new ValidationException(new ConstraintViolationList([$violation]));
$error = Error::createFromException($exception, 422);

$this->assertSame('An error occurred', $error->getTitle());
$this->assertSame($exception->getMessage(), $error->getDetail());
$this->assertSame(422, $error->getStatus());
}

public function testErrorProviderProduction(): void
{
$provider = new ErrorProvider(debug: false);
$request = Request::create('/');
$request->attributes->set('exception', new \Exception());
/** @var \ApiPlatform\State\ApiResource\Error */
/** @var Error */
$error = $provider->provide(new Get(), [], ['request' => $request]);
$this->assertEquals('Internal Server Error', $error->getDetail());
}
Expand Down
2 changes: 2 additions & 0 deletions src/Validator/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function __construct(string|ConstraintViolationListInterface $message = n
if ($message instanceof ConstraintViolationListInterface) {
$this->constraintViolationList = $message;
parent::__construct($this->__toString(), $code ?? 0, $previous);
$this->detail = $this->getMessage();

return;
}
Expand All @@ -119,6 +120,7 @@ public function __construct(string|ConstraintViolationListInterface $message = n

trigger_deprecation('api_platform/core', '5.0', \sprintf('The "%s" exception will have a "%s" first argument in 5.x.', self::class, ConstraintViolationListInterface::class));
parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
$this->detail = $this->getMessage();
}

public function getId(): string
Expand Down
Loading