Repository navigation
Expand file tree
/
Copy pathExceptionMappingTable.php
More file actions
120 lines (107 loc) · 4.08 KB
/
Copy pathExceptionMappingTable.php
File metadata and controls
120 lines (107 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\ErrorHandler;
use Throwable;
use TinyBlocks\Http\ErrorHandler\Internal\Mapping\AnyExactClassMatcher;
use TinyBlocks\Http\ErrorHandler\Internal\Mapping\ExactClassMatcher;
use TinyBlocks\Http\ErrorHandler\Internal\Mapping\SubclassMatcher;
/**
* Fluent table of exception-to-MappedError rules, evaluated in registration order. The first
* matching rule wins, and unmatched exceptions return <code>null</code> to let the middleware
* decide between fallback and rethrow.
*/
final readonly class ExceptionMappingTable
{
private function __construct(private array $entries)
{
}
/**
* Creates an empty ExceptionMappingTable.
*
* @return ExceptionMappingTable The empty table.
*/
public static function create(): ExceptionMappingTable
{
return new ExceptionMappingTable(entries: []);
}
/**
* Registers a rule for an exact exception class match.
*
* @param class-string<Throwable> $exceptionClass The exception class to match exactly.
* @return ExceptionMappingRule The intermediate rule builder.
*/
public function when(string $exceptionClass): ExceptionMappingRule
{
return new ExceptionMappingRule(
table: $this,
matcher: new ExactClassMatcher(exceptionClass: $exceptionClass)
);
}
/**
* Returns the mapped error of the first rule that matches the exception, or null when none matches.
*
* @param Throwable $exception The exception to translate.
* @return MappedError|null The mapped error, or <code>null</code> when no registered rule matches.
*/
public function mapTo(Throwable $exception): ?MappedError
{
foreach ($this->entries as $entry) {
$mappedError = $entry->resolve($exception);
if (!is_null($mappedError)) {
return $mappedError;
}
}
return null;
}
/**
* Registers a rule matching any of the given exception classes exactly.
*
* @param non-empty-list<class-string<Throwable>> $exceptionClasses The exception classes to match against.
* @return ExceptionMappingRule The intermediate rule builder.
*/
public function whenAny(array $exceptionClasses): ExceptionMappingRule
{
return new ExceptionMappingRule(
table: $this,
matcher: new AnyExactClassMatcher(exceptionClasses: $exceptionClasses)
);
}
/**
* Appends an entry and returns the resulting table.
*
* <p>Not part of the consumer-facing API. Used only by {@see ExceptionMappingRule} to append entries.</p>
*
* @param MappingEntry $entry The mapping entry to append.
* @return ExceptionMappingTable The resulting table with the entry added.
*/
public function withEntry(MappingEntry $entry): ExceptionMappingTable
{
return new ExceptionMappingTable(entries: [...$this->entries, $entry]);
}
/**
* Returns a new table with this table's rules followed by the other table's rules.
*
* <p>The combined table keeps first-match-wins across both, so the rules of this table take
* precedence over the rules of <code>$other</code> for an exception both would match.</p>
*
* @param ExceptionMappingTable $other The table whose rules are appended after this table's.
* @return ExceptionMappingTable The combined table.
*/
public function mergedWith(ExceptionMappingTable $other): ExceptionMappingTable
{
return new ExceptionMappingTable(entries: [...$this->entries, ...$other->entries]);
}
/**
* Registers a rule matching any subclass of the given base exception class.
*
* @param class-string<Throwable> $baseException The base exception class to match against.
* @return ExceptionMappingRule The intermediate rule builder.
*/
public function whenSubclassOf(string $baseException): ExceptionMappingRule
{
return new ExceptionMappingRule(
table: $this,
matcher: new SubclassMatcher(baseException: $baseException)
);
}
}