Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
No results found
Select Git revision
Show changes
Commits on Source (2)
......@@ -35,6 +35,9 @@
"symfony/phpunit-bridge": "^5.3",
"vimeo/psalm": "^4.4"
},
"conflict": {
"symfony/dependency-injection": "5.3.7"
},
"autoload": {
"psr-4": {
"Dbp\\Relay\\CoreBundle\\": "src/"
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "19c951b51140323b7c4efc5d09b38c19",
"content-hash": "06fcdc0b0163095f806f3dcf9986c508",
"packages": [
{
"name": "api-platform/core",
......
......@@ -13,50 +13,34 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
*/
class ApiError extends HttpException
{
/**
* @var ?string
*/
private $errorId;
/**
* @var ?array
*/
private $errorDetails;
private const WITHDETAILSSTATUS = -1;
public function __construct(int $statusCode, ?string $message = '', \Throwable $previous = null, array $headers = [], ?int $code = 0)
{
$message = json_decode($message, true);
if ($message === null) {
$message = [
if ($statusCode === self::WITHDETAILSSTATUS) {
$decoded = json_decode($message, true, 512, JSON_THROW_ON_ERROR);
$statusCode = $decoded['statusCode'];
unset($decoded['statusCode']);
} else {
$decoded = [
'message' => $message,
'errorId' => '',
'errorDetails' => null,
];
}
parent::__construct($statusCode, json_encode($message), $previous, $headers, $code);
}
public function setErrorDetails(string $errorId, array $errorDetails = [])
{
$this->errorId = $errorId;
$this->errorDetails = $errorDetails;
}
public function getErrorDetails(): array
{
return [$this->errorId, $this->errorDetails];
parent::__construct($statusCode, json_encode($decoded), $previous, $headers, $code);
}
public static function withDetails(int $statusCode, ?string $message = '', string $errorId = '', array $errorDetails = [])
{
$message = [
'statusCode' => $statusCode,
'message' => $message,
'errorId' => $errorId,
'errorDetails' => $errorDetails,
];
return new ApiError($statusCode, json_encode($message));
return new ApiError(self::WITHDETAILSSTATUS, json_encode($message));
}
}
<?php
declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Tests;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use PHPUnit\Framework\TestCase;
class ApiErrorTest extends TestCase
{
public function testBasics()
{
$error = new ApiError(400, 'foobar');
$message = json_decode($error->getMessage(), true);
$this->assertSame('foobar', $message['message']);
$this->assertSame(400, $error->getStatusCode());
}
public function testWithDetails()
{
$error = ApiError::withDetails(424, 'message', 'id', ['foo' => 'bar']);
$message = json_decode($error->getMessage(), true);
$this->assertSame('message', $message['message']);
$this->assertSame('id', $message['errorId']);
$this->assertSame(['foo' => 'bar'], $message['errorDetails']);
$this->assertSame(424, $error->getStatusCode());
}
}