17
17
18
18
package org .openqa .selenium .grid .docker ;
19
19
20
+ import static java .util .Optional .ofNullable ;
21
+ import static org .openqa .selenium .docker .ContainerConfig .image ;
22
+ import static org .openqa .selenium .remote .Dialect .W3C ;
23
+ import static org .openqa .selenium .remote .http .Contents .string ;
24
+ import static org .openqa .selenium .remote .http .HttpMethod .GET ;
25
+ import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
26
+
20
27
import org .openqa .selenium .Capabilities ;
21
28
import org .openqa .selenium .Dimension ;
22
29
import org .openqa .selenium .ImmutableCapabilities ;
30
+ import org .openqa .selenium .RetrySessionRequestException ;
23
31
import org .openqa .selenium .SessionNotCreatedException ;
24
32
import org .openqa .selenium .TimeoutException ;
33
+ import org .openqa .selenium .WebDriverException ;
25
34
import org .openqa .selenium .docker .Container ;
26
35
import org .openqa .selenium .docker .ContainerInfo ;
27
36
import org .openqa .selenium .docker .Docker ;
30
39
import org .openqa .selenium .grid .data .CreateSessionRequest ;
31
40
import org .openqa .selenium .grid .node .ActiveSession ;
32
41
import org .openqa .selenium .grid .node .SessionFactory ;
42
+ import org .openqa .selenium .internal .Either ;
33
43
import org .openqa .selenium .internal .Require ;
34
44
import org .openqa .selenium .json .Json ;
35
45
import org .openqa .selenium .net .PortProber ;
71
81
import java .util .logging .Level ;
72
82
import java .util .logging .Logger ;
73
83
74
- import static java .util .Optional .ofNullable ;
75
- import static org .openqa .selenium .docker .ContainerConfig .image ;
76
- import static org .openqa .selenium .remote .Dialect .W3C ;
77
- import static org .openqa .selenium .remote .http .Contents .string ;
78
- import static org .openqa .selenium .remote .http .HttpMethod .GET ;
79
- import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
80
-
81
84
public class DockerSessionFactory implements SessionFactory {
82
85
83
86
private static final Logger LOG = Logger .getLogger (DockerSessionFactory .class .getName ());
@@ -122,7 +125,7 @@ public boolean test(Capabilities capabilities) {
122
125
}
123
126
124
127
@ Override
125
- public Optional < ActiveSession > apply (CreateSessionRequest sessionRequest ) {
128
+ public Either < WebDriverException , ActiveSession > apply (CreateSessionRequest sessionRequest ) {
126
129
LOG .info ("Starting session for " + sessionRequest .getCapabilities ());
127
130
int port = PortProber .findFreePort ();
128
131
URL remoteAddress = getUrl (port );
@@ -158,9 +161,10 @@ public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {
158
161
span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
159
162
160
163
container .stop (Duration .ofMinutes (1 ));
161
- LOG .warning (String .format (
162
- "Unable to connect to docker server (container id: %s)" , container .getId ()));
163
- return Optional .empty ();
164
+ String message = String .format (
165
+ "Unable to connect to docker server (container id: %s)" , container .getId ());
166
+ LOG .warning (message );
167
+ return Either .left (new RetrySessionRequestException (message ));
164
168
}
165
169
LOG .info (String .format ("Server is ready (container id: %s)" , container .getId ()));
166
170
@@ -187,8 +191,9 @@ public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {
187
191
span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
188
192
189
193
container .stop (Duration .ofMinutes (1 ));
190
- LOG .log (Level .WARNING , "Unable to create session: " + e .getMessage (), e );
191
- return Optional .empty ();
194
+ String message = "Unable to create session: " + e .getMessage ();
195
+ LOG .log (Level .WARNING , message , e );
196
+ return Either .left (new SessionNotCreatedException (message ));
192
197
}
193
198
194
199
SessionId id = new SessionId (response .getSessionId ());
@@ -221,7 +226,7 @@ public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {
221
226
id ,
222
227
capabilities ,
223
228
container .getId ()));
224
- return Optional . of (new DockerSession (
229
+ return Either . right (new DockerSession (
225
230
container ,
226
231
videoContainer ,
227
232
tracer ,
0 commit comments