1 | <?php |
||
22 | final class CustomErrorMiddleware implements Middleware |
||
23 | { |
||
24 | /** @var \Closure(Error, RequestFailed) : \Exception */ |
||
25 | private $factory; |
||
26 | |||
27 | /** |
||
28 | * @param \Closure(Error, RequestFailed) : \Exception $factory |
||
29 | */ |
||
30 | 12 | private function __construct($factory) |
|
34 | |||
35 | /** |
||
36 | * Creates the middleware from a provided closure which receives |
||
37 | * `Tarantool\Client\Error` and `Tarantool\Client\Exception\RequestFailed` |
||
38 | * objects as arguments and should return a custom exception. |
||
39 | * |
||
40 | * Example: |
||
41 | * |
||
42 | * ```php |
||
43 | * $middleware = CustomErrorMiddleware::fromFactory( |
||
44 | * static function (Error $err, RequestFailed $ex) : \Exception { |
||
45 | * return 'UserNotFound' === $err->tryGetField('custom_type') |
||
46 | * ? new UserNotFound($err->getMessage(), $err->getCode()) |
||
47 | * : $ex; |
||
48 | * } |
||
49 | * ); |
||
50 | * ``` |
||
51 | */ |
||
52 | 3 | public static function fromFactory(\Closure $factory) : self |
|
60 | |||
61 | /** |
||
62 | * Creates the middleware from an array in which the keys are custom types |
||
63 | * of box.error objects and the corresponding values are fully qualified names |
||
64 | * of custom exception classes. |
||
65 | * |
||
66 | * Example: |
||
67 | * |
||
68 | * ```php |
||
69 | * $middleware = CustomErrorMiddleware::fromMapping([ |
||
70 | * 'UserNotFound' => UserNotFound::class, |
||
71 | * 'MyCustomType' => MyCustomException::class, |
||
72 | * ... |
||
73 | * ]); |
||
74 | * ``` |
||
75 | * |
||
76 | * @param array<string, class-string<\Exception>> $mapping |
||
77 | */ |
||
78 | 6 | public static function fromMapping(array $mapping) : self |
|
94 | |||
95 | /** |
||
96 | * Creates the middleware from the base namespace for the custom exception classes. |
||
97 | * The exception class name then will be in the format of "<namespace>\<lua_error.custom_type>". |
||
98 | * |
||
99 | * Example: |
||
100 | * |
||
101 | * ```php |
||
102 | * $middleware = CustomErrorMiddleware::fromNamespace('Foo\Bar'); |
||
103 | * ``` |
||
104 | */ |
||
105 | 3 | public static function fromNamespace(string $namespace) : self |
|
125 | |||
126 | 12 | public function process(Request $request, Handler $handler) : Response |
|
138 | } |
||
139 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.