-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCustomErrorMiddleware.php
138 lines (123 loc) · 4.14 KB
/
CustomErrorMiddleware.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This file is part of the tarantool/client package.
*
* (c) Eugene Leonovich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Tarantool\Client\Middleware;
use Tarantool\Client\Error;
use Tarantool\Client\Exception\RequestFailed;
use Tarantool\Client\Handler\Handler;
use Tarantool\Client\Request\Request;
use Tarantool\Client\Response;
final class CustomErrorMiddleware implements Middleware
{
/** @var \Closure(Error, RequestFailed) : \Exception */
private $factory;
/**
* @param \Closure(Error, RequestFailed) : \Exception $factory
*/
private function __construct($factory)
{
$this->factory = $factory;
}
/**
* Creates the middleware from a provided closure which receives
* `Tarantool\Client\Error` and `Tarantool\Client\Exception\RequestFailed`
* objects as arguments and should return a custom exception.
*
* Example:
*
* ```php
* $middleware = CustomErrorMiddleware::fromFactory(
* static function (Error $err, RequestFailed $ex) : \Exception {
* return 'UserNotFound' === $err->tryGetField('custom_type')
* ? new UserNotFound($err->getMessage(), $err->getCode())
* : $ex;
* }
* );
* ```
*/
public static function fromFactory(\Closure $factory) : self
{
return new self(
static function (Error $err, RequestFailed $ex) use ($factory) : \Exception {
return $factory($err, $ex);
}
);
}
/**
* Creates the middleware from an array in which the keys are custom types
* of box.error objects and the corresponding values are fully qualified names
* of custom exception classes.
*
* Example:
*
* ```php
* $middleware = CustomErrorMiddleware::fromMapping([
* 'UserNotFound' => UserNotFound::class,
* 'MyCustomType' => MyCustomException::class,
* ...
* ]);
* ```
*
* @param array<string, class-string<\Exception>> $mapping
*/
public static function fromMapping(array $mapping) : self
{
return new self(
static function (Error $err, RequestFailed $ex) use ($mapping) : \Exception {
do {
$customType = $err->tryGetField('custom_type');
if ($customType && isset($mapping[$customType])) {
/** @psalm-suppress UnsafeInstantiation */
return new $mapping[$customType]($err->getMessage(), $err->getCode());
}
} while ($err = $err->getPrevious());
return $ex;
}
);
}
/**
* Creates the middleware from the base namespace for the custom exception classes.
* The exception class name then will be in the format of "<namespace>\<lua_error.custom_type>".
*
* Example:
*
* ```php
* $middleware = CustomErrorMiddleware::fromNamespace('Foo\Bar');
* ```
*/
public static function fromNamespace(string $namespace) : self
{
$namespace = \rtrim($namespace, '\\').'\\';
return new self(
static function (Error $err, RequestFailed $ex) use ($namespace) : \Exception {
if (!$customType = $err->tryGetField('custom_type')) {
return $ex;
}
/** @var class-string<\Exception> $className */
$className = $namespace.$customType;
/** @psalm-suppress UnsafeInstantiation */
return \class_exists($className)
? new $className($err->getMessage(), $err->getCode())
: $ex;
}
);
}
public function process(Request $request, Handler $handler) : Response
{
try {
return $handler->handle($request);
} catch (RequestFailed $e) {
if ($error = $e->getError()) {
throw ($this->factory)($error, $e);
}
throw $e;
}
}
}