23
23
import org .openqa .selenium .ImmutableCapabilities ;
24
24
import org .openqa .selenium .Proxy ;
25
25
import org .openqa .selenium .SessionNotCreatedException ;
26
- import org .openqa .selenium .WebDriverException ;
27
26
import org .openqa .selenium .internal .Either ;
28
27
import org .openqa .selenium .internal .Require ;
29
28
import org .openqa .selenium .json .Json ;
@@ -60,21 +59,19 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
60
59
desired = desired == null ? new ImmutableCapabilities () : desired ;
61
60
62
61
try (NewSessionPayload payload = NewSessionPayload .create (desired )) {
63
- Either <WebDriverException , Result > result = createSession (client , payload );
62
+ Either <SessionNotCreatedException , Result > result = createSession (client , payload );
64
63
65
64
if (result .isRight ()) {
66
65
Result toReturn = result .right ();
67
66
LOG .info (String .format ("Detected dialect: %s" , toReturn .dialect ));
68
67
return toReturn ;
69
68
} else {
70
- throw new SessionNotCreatedException (
71
- String .format ("Unable to create new remote session with desired capabilities = %s" , desired ),
72
- result .left ());
69
+ throw result .left ();
73
70
}
74
71
}
75
72
}
76
73
77
- public Either <WebDriverException , Result > createSession (HttpHandler client , NewSessionPayload payload ) throws IOException {
74
+ public Either <SessionNotCreatedException , Result > createSession (HttpHandler client , NewSessionPayload payload ) throws IOException {
78
75
int threshold = (int ) Math .min (Runtime .getRuntime ().freeMemory () / 10 , Integer .MAX_VALUE );
79
76
FileBackedOutputStream os = new FileBackedOutputStream (threshold );
80
77
@@ -91,7 +88,7 @@ public Either<WebDriverException, Result> createSession(HttpHandler client, NewS
91
88
}
92
89
}
93
90
94
- private Either <WebDriverException , Result > createSession (HttpHandler client , InputStream newSessionBlob , long size ) {
91
+ private Either <SessionNotCreatedException , Result > createSession (HttpHandler client , InputStream newSessionBlob , long size ) {
95
92
// Create the http request and send it
96
93
HttpRequest request = new HttpRequest (HttpMethod .POST , "/session" );
97
94
@@ -111,7 +108,7 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
111
108
try {
112
109
blob = new Json ().toType (string (response ), Map .class );
113
110
} catch (JsonException e ) {
114
- return Either .left (new WebDriverException (
111
+ return Either .left (new SessionNotCreatedException (
115
112
"Unable to parse remote response: " + string (response ), e ));
116
113
}
117
114
@@ -121,9 +118,13 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
121
118
blob );
122
119
123
120
if (initialResponse .getStatusCode () != 200 ) {
124
- return Either .left (new WebDriverException (
125
- String .format ("Server response code %s, message: %s" ,
126
- initialResponse .getStatusCode (), blob .get ("message" ).toString ())));
121
+ Object rawResponseValue = initialResponse .getData ().get ("value" );
122
+ String responseMessage = rawResponseValue instanceof Map
123
+ ? ((Map <?, ?>) rawResponseValue ).get ("message" ).toString ()
124
+ : new Json ().toJson (rawResponseValue );
125
+ return Either .left (new SessionNotCreatedException (
126
+ String .format ("Response code %s. Message: %s" ,
127
+ initialResponse .getStatusCode (), responseMessage )));
127
128
}
128
129
129
130
return Stream .of (
@@ -132,9 +133,9 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
132
133
.map (func -> func .apply (initialResponse ))
133
134
.filter (Objects ::nonNull )
134
135
.findFirst ()
135
- .<Either <WebDriverException , Result >>map (Either ::right )
136
+ .<Either <SessionNotCreatedException , Result >>map (Either ::right )
136
137
.orElseGet (() -> Either .left (
137
- new WebDriverException ("Handshake response does not match any supported protocol" )));
138
+ new SessionNotCreatedException ("Handshake response does not match any supported protocol" )));
138
139
}
139
140
140
141
public static class Result {
0 commit comments