Skip to content

Commit b72a9d0

Browse files
committed
feat: enhance GenericErrorFormatter with customizable format
1 parent ff5bea0 commit b72a9d0

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

src/Formatter/GenericErrorFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
final class GenericErrorFormatter extends AbstractErrorFormatter
1111
{
12+
/**
13+
* @var array<string, mixed>
14+
*/
1215
private array $format = [
1316
'message' => '{title}',
1417
'status' => '{status_code}',

tests/ErrorFormatterTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
use Jenky\ApiError\Formatter\GenericErrorFormatter;
88
use Jenky\ApiError\Formatter\Rfc7807ErrorFormatter;
9+
use Jenky\ApiError\GenericProblem;
10+
use Jenky\ApiError\Problem;
11+
use Jenky\ApiError\Rfc7807Problem;
12+
use Jenky\ApiError\Transformer\ExceptionTransformer;
913
use PHPUnit\Framework\Attributes\DataProvider;
1014
use PHPUnit\Framework\TestCase;
1115

@@ -37,14 +41,15 @@ public function test_generic_error_formatter(\Throwable $exception, bool $debug)
3741
#[DataProvider('provideExceptions')]
3842
public function test_rfc7808_error_formatter(\Throwable $exception, bool $debug): void
3943
{
40-
$formatter = new Rfc7807ErrorFormatter($debug);
44+
$formatter = new Rfc7807ErrorFormatter($debug, new Rfc7807Transformer());
4145

4246
$data = $formatter->format($exception)->data;
4347

4448
$this->assertIsArray($data);
4549
$this->assertArrayHasKey('type', $data);
4650
$this->assertArrayHasKey('title', $data);
4751
$this->assertArrayHasKey('status', $data);
52+
$this->assertArrayHasKey('invalid-params', $data);
4853

4954
if ($debug) {
5055
$this->assertArrayHasKey('debug', $data);
@@ -57,11 +62,33 @@ public function test_rfc7808_error_formatter(\Throwable $exception, bool $debug)
5762
$this->assertSame($data['detail'], $exception->getMessage());
5863
}
5964

60-
$this->assertSame($data['type'], 'about:blank');
65+
$this->assertSame($data['type'], 'http://localhost');
6166
$this->assertSame($data['title'], 'Internal Server Error');
6267
$this->assertSame($data['status'], 500);
6368
}
6469

70+
#[DataProvider('provideExceptions')]
71+
public function test_custom_formatter(\Throwable $exception, bool $debug): void
72+
{
73+
$formatter = new GenericErrorFormatter(transformer: new ExceptionClassTransformer());
74+
$formatter->setFormat([
75+
'message' => '{title}',
76+
'status' => '{status_code}',
77+
'class' => '{class}',
78+
]);
79+
80+
$data = $formatter->format($exception)->data;
81+
82+
$this->assertIsArray($data);
83+
$this->assertArrayHasKey('message', $data);
84+
$this->assertArrayHasKey('status', $data);
85+
$this->assertArrayHasKey('class', $data);
86+
87+
$this->assertSame($data['message'], $exception->getMessage() ?: 'Internal Server Error');
88+
$this->assertSame($data['status'], 500);
89+
$this->assertSame($data['class'], \get_class($exception));
90+
}
91+
6592
public static function provideExceptions(): iterable
6693
{
6794
yield [new \RuntimeException(), false];
@@ -74,3 +101,29 @@ public static function provideExceptions(): iterable
74101
yield [new \InvalidArgumentException('', 100), true];
75102
}
76103
}
104+
105+
final class ExceptionClassTransformer implements ExceptionTransformer
106+
{
107+
public function transform(\Throwable $exception): Problem
108+
{
109+
$problem = GenericProblem::createFromThrowable($exception);
110+
111+
$problem->set('class', $problem->getClass());
112+
113+
return $problem;
114+
}
115+
}
116+
117+
final class Rfc7807Transformer implements ExceptionTransformer
118+
{
119+
public function transform(\Throwable $exception): Problem
120+
{
121+
$problem = Rfc7807Problem::createFromThrowable($exception);
122+
123+
$problem->setType('http://localhost')
124+
->addInvalidParam('foo', 'bar')
125+
->addInvalidParam('quiz', 'qux');
126+
127+
return $problem;
128+
}
129+
}

tests/ProblemTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,19 @@ public static function provideRfc7807ProblemExceptions(): iterable
100100
],
101101
];
102102
}
103+
104+
public function test_rfc7807_setters(): void
105+
{
106+
$problem = Rfc7807Problem::createFromThrowable(new \RuntimeException('foo'));
107+
108+
$this->expectException(\InvalidArgumentException::class);
109+
110+
$problem->setType('bar');
111+
112+
$problem->addInvalidParam('foo', 'bar');
113+
$context = $problem->context();
114+
115+
$this->assertArrayHasKey('invalid_params', $context);
116+
$this->assertEquals($context['invalid_params']['foo'], 'bar');
117+
}
103118
}

0 commit comments

Comments
 (0)