7
7
use ScriptFUSION \Porter \Cache \CacheUnavailableException ;
8
8
use ScriptFUSION \Porter \Connector \Recoverable \RecoverableExceptionHandler ;
9
9
use ScriptFUSION \Porter \Connector \Recoverable \StatelessRecoverableExceptionHandler ;
10
+ use ScriptFUSION \Porter \ExceptionDescriptor ;
10
11
11
12
/**
12
13
* Connector whose lifecycle is synchronised with an import operation. Ensures correct ConnectionContext is delivered
@@ -27,21 +28,26 @@ final class ImportConnector implements ConnectorWrapper
27
28
*
28
29
* @var RecoverableExceptionHandler
29
30
*/
30
- private $ userReh ;
31
+ private $ userExceptionHandler ;
31
32
32
33
/**
33
34
* Resource-defined exception handler called when a recoverable exception is thrown by Connector::fetch().
34
35
*
35
36
* @var RecoverableExceptionHandler
36
37
*/
37
- private $ resourceReh ;
38
+ private $ resourceExceptionHandler ;
38
39
39
40
private $ maxFetchAttempts ;
40
41
42
+ /**
43
+ * @var ExceptionDescriptor[]
44
+ */
45
+ private $ recoverableExceptionDescriptors ;
46
+
41
47
/**
42
48
* @param Connector|AsyncConnector $connector Wrapped connector.
43
49
* @param ConnectionContext $connectionContext Connection context.
44
- * @param RecoverableExceptionHandler $recoverableExceptionHandler
50
+ * @param RecoverableExceptionHandler $recoverableExceptionHandler User's recoverable exception handler.
45
51
* @param int $maxFetchAttempts
46
52
*/
47
53
public function __construct (
@@ -56,7 +62,7 @@ public function __construct(
56
62
57
63
$ this ->connector = clone $ connector ;
58
64
$ this ->connectionContext = $ connectionContext ;
59
- $ this ->userReh = $ recoverableExceptionHandler ;
65
+ $ this ->userExceptionHandler = $ recoverableExceptionHandler ;
60
66
$ this ->maxFetchAttempts = $ maxFetchAttempts ;
61
67
}
62
68
@@ -92,20 +98,35 @@ private function createExceptionHandler(): \Closure
92
98
93
99
return function (\Exception $ exception ) use (&$ userHandlerCloned , &$ resourceHandlerCloned ): void {
94
100
// Throw exception instead of retrying, if unrecoverable.
95
- if (!$ exception instanceof RecoverableConnectorException ) {
101
+ if (!$ this -> isRecoverable ( $ exception) ) {
96
102
throw $ exception ;
97
103
}
98
104
99
105
// Call resource's exception handler, if defined.
100
- if ($ this ->resourceReh ) {
101
- self ::invokeHandler ($ this ->resourceReh , $ exception , $ resourceHandlerCloned );
106
+ if ($ this ->resourceExceptionHandler ) {
107
+ self ::invokeHandler ($ this ->resourceExceptionHandler , $ exception , $ resourceHandlerCloned );
102
108
}
103
109
104
110
// Call user's exception handler.
105
- self ::invokeHandler ($ this ->userReh , $ exception , $ userHandlerCloned );
111
+ self ::invokeHandler ($ this ->userExceptionHandler , $ exception , $ userHandlerCloned );
106
112
};
107
113
}
108
114
115
+ private function isRecoverable (\Exception $ exception ): bool
116
+ {
117
+ if ($ exception instanceof RecoverableConnectorException) {
118
+ return true ;
119
+ }
120
+
121
+ foreach ($ this ->recoverableExceptionDescriptors as $ exceptionDescriptor ) {
122
+ if ($ exceptionDescriptor ->matches ($ exception )) {
123
+ return true ;
124
+ }
125
+ }
126
+
127
+ return false ;
128
+ }
129
+
109
130
/**
110
131
* Invokes the specified fetch exception handler, cloning it if required.
111
132
*
@@ -163,10 +184,20 @@ public function findBaseConnector()
163
184
*/
164
185
public function setRecoverableExceptionHandler (RecoverableExceptionHandler $ recoverableExceptionHandler ): void
165
186
{
166
- if ($ this ->resourceReh !== null ) {
187
+ if ($ this ->resourceExceptionHandler !== null ) {
167
188
throw new \LogicException ('Cannot set resource \'s recoverable exception handler: already set! ' );
168
189
}
169
190
170
- $ this ->resourceReh = $ recoverableExceptionHandler ;
191
+ $ this ->resourceExceptionHandler = $ recoverableExceptionHandler ;
192
+ }
193
+
194
+ /**
195
+ * Adds the specified exception descriptor, designating it as a recoverable exception type.
196
+ *
197
+ * @param ExceptionDescriptor $descriptor
198
+ */
199
+ public function addRecoverableExceptionDescriptor (ExceptionDescriptor $ descriptor ): void
200
+ {
201
+ $ this ->recoverableExceptionDescriptors [] = $ descriptor ;
171
202
}
172
203
}
0 commit comments