Skip to content

Commit cb026af

Browse files
committed
refactor: create custom exception for invalid payload format
1 parent 7f42d70 commit cb026af

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir PHP Runtime.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Runtime\Exception;
15+
16+
/**
17+
* Exception thrown when an invalid format version is used.
18+
*/
19+
class InvalidFormatVersionException extends \InvalidArgumentException implements ExceptionInterface
20+
{
21+
/**
22+
* Constructor.
23+
*/
24+
public function __construct(string $formatVersion)
25+
{
26+
parent::__construct(sprintf('Format must be either "1.0" or "2.0", "%s" given', $formatVersion));
27+
}
28+
}

src/Lambda/Response/Http/HttpResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Tightenco\Collect\Support\Arr;
1717
use Tightenco\Collect\Support\Collection;
18+
use Ymir\Runtime\Exception\InvalidFormatVersionException;
1819
use Ymir\Runtime\Lambda\Response\ResponseInterface;
1920

2021
/**
@@ -65,7 +66,7 @@ class HttpResponse implements ResponseInterface
6566
public function __construct(string $body, array $headers = [], int $statusCode = 200, string $formatVersion = '1.0', bool $compressible = true)
6667
{
6768
if (!in_array($formatVersion, ['1.0', '2.0'])) {
68-
throw new \InvalidArgumentException('"formatVersion" must be either "1.0" or "2.0"');
69+
throw new InvalidFormatVersionException($formatVersion);
6970
}
7071

7172
$this->body = $body;

tests/Unit/Lambda/Response/Http/HttpResponseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414
namespace Ymir\Runtime\Tests\Unit\Lambda\Response\Http;
1515

1616
use PHPUnit\Framework\TestCase;
17+
use Ymir\Runtime\Exception\InvalidFormatVersionException;
1718
use Ymir\Runtime\Lambda\Response\Http\HttpResponse;
1819

1920
class HttpResponseTest extends TestCase
2021
{
22+
public function testConstructorThrowsExceptionWithInvalidFormatVersion(): void
23+
{
24+
$this->expectException(InvalidFormatVersionException::class);
25+
26+
new HttpResponse('foo', [], 200, '3.0');
27+
}
28+
2129
public function testGetResponseDataWithFormatVersion1And304Status(): void
2230
{
2331
$response = new HttpResponse('foo', [], 304);

0 commit comments

Comments
 (0)