2323import org .openqa .selenium .ImmutableCapabilities ;
2424import org .openqa .selenium .Proxy ;
2525import org .openqa .selenium .SessionNotCreatedException ;
26- import org .openqa .selenium .WebDriverException ;
2726import org .openqa .selenium .internal .Either ;
2827import org .openqa .selenium .internal .Require ;
2928import org .openqa .selenium .json .Json ;
@@ -60,21 +59,19 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
6059 desired = desired == null ? new ImmutableCapabilities () : desired ;
6160
6261 try (NewSessionPayload payload = NewSessionPayload .create (desired )) {
63- Either <WebDriverException , Result > result = createSession (client , payload );
62+ Either <SessionNotCreatedException , Result > result = createSession (client , payload );
6463
6564 if (result .isRight ()) {
6665 Result toReturn = result .right ();
6766 LOG .info (String .format ("Detected dialect: %s" , toReturn .dialect ));
6867 return toReturn ;
6968 } 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 ();
7370 }
7471 }
7572 }
7673
77- public Either <WebDriverException , Result > createSession (HttpHandler client , NewSessionPayload payload ) throws IOException {
74+ public Either <SessionNotCreatedException , Result > createSession (HttpHandler client , NewSessionPayload payload ) throws IOException {
7875 int threshold = (int ) Math .min (Runtime .getRuntime ().freeMemory () / 10 , Integer .MAX_VALUE );
7976 FileBackedOutputStream os = new FileBackedOutputStream (threshold );
8077
@@ -91,7 +88,7 @@ public Either<WebDriverException, Result> createSession(HttpHandler client, NewS
9188 }
9289 }
9390
94- private Either <WebDriverException , Result > createSession (HttpHandler client , InputStream newSessionBlob , long size ) {
91+ private Either <SessionNotCreatedException , Result > createSession (HttpHandler client , InputStream newSessionBlob , long size ) {
9592 // Create the http request and send it
9693 HttpRequest request = new HttpRequest (HttpMethod .POST , "/session" );
9794
@@ -111,7 +108,7 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
111108 try {
112109 blob = new Json ().toType (string (response ), Map .class );
113110 } catch (JsonException e ) {
114- return Either .left (new WebDriverException (
111+ return Either .left (new SessionNotCreatedException (
115112 "Unable to parse remote response: " + string (response ), e ));
116113 }
117114
@@ -121,9 +118,13 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
121118 blob );
122119
123120 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 )));
127128 }
128129
129130 return Stream .of (
@@ -132,9 +133,9 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
132133 .map (func -> func .apply (initialResponse ))
133134 .filter (Objects ::nonNull )
134135 .findFirst ()
135- .<Either <WebDriverException , Result >>map (Either ::right )
136+ .<Either <SessionNotCreatedException , Result >>map (Either ::right )
136137 .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" )));
138139 }
139140
140141 public static class Result {
0 commit comments