@@ -29,6 +29,7 @@ const error = require('../error');
29
29
const cmd = require ( '../lib/command' ) ;
30
30
const logging = require ( '../lib/logging' ) ;
31
31
const promise = require ( '../lib/promise' ) ;
32
+ const Session = require ( '../lib/session' ) . Session ;
32
33
33
34
34
35
@@ -432,40 +433,62 @@ class Executor {
432
433
log . finer ( ( ) => '>>>\n' + request ) ;
433
434
return this . client_ . send ( request ) . then ( function ( response ) {
434
435
log . finer ( ( ) => '<<<\n' + response ) ;
435
- return parseHttpResponse ( /** @type {!HttpResponse } */ ( response ) ) ;
436
+
437
+ let value = parseHttpResponse ( /** @type {!HttpResponse } */ ( response ) ) ;
438
+ let isResponseObj = ( value && typeof value === 'object' ) ;
439
+ if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
440
+ if ( ! isResponseObj ) {
441
+ throw new error . WebDriverError (
442
+ 'Unable to parse new session response: ' + response . body ) ;
443
+ }
444
+ return new Session ( value [ 'sessionId' ] , value [ 'value' ] ) ;
445
+ }
446
+ return isResponseObj ? value [ 'value' ] : value ;
436
447
} ) ;
437
448
}
438
449
}
439
450
440
451
452
+ /**
453
+ * @param {string } str .
454
+ * @return {? } .
455
+ */
456
+ function tryParse ( str ) {
457
+ try {
458
+ return JSON . parse ( str ) ;
459
+ } catch ( ignored ) {
460
+ // Do nothing.
461
+ }
462
+ }
463
+
464
+
441
465
/**
442
466
* Callback used to parse {@link HttpResponse} objects from a
443
467
* {@link HttpClient}.
444
468
* @param {!HttpResponse } httpResponse The HTTP response to parse.
445
469
* @return {!Object } The parsed response.
446
470
*/
447
471
function parseHttpResponse ( httpResponse ) {
448
- try {
449
- return /** @type {!Object } */ ( JSON . parse ( httpResponse . body ) ) ;
450
- } catch ( ignored ) {
451
- // Whoops, looks like the server sent us a malformed response. We'll need
452
- // to manually build a response object based on the response code.
472
+ let parsed = tryParse ( httpResponse . body ) ;
473
+ if ( parsed !== undefined ) {
474
+ error . checkLegacyResponse ( parsed ) ;
475
+ return parsed ;
476
+ }
477
+
478
+ let value = httpResponse . body . replace ( / \r \n / g, '\n' ) ;
479
+ if ( ! value ) {
480
+ return null ;
453
481
}
454
482
455
- let response = {
456
- 'status' : error . ErrorCode . SUCCESS ,
457
- 'value' : httpResponse . body . replace ( / \r \n / g, '\n' )
458
- } ;
459
-
460
- if ( httpResponse . status >= 400 ) {
461
- // 404 represents an unknown command; anything else is a generic unknown
462
- // error.
463
- response [ 'status' ] = httpResponse . status == 404 ?
464
- error . ErrorCode . UNKNOWN_COMMAND :
465
- error . ErrorCode . UNKNOWN_ERROR ;
483
+ // 404 represents an unknown command; anything else is a generic unknown
484
+ // error.
485
+ if ( httpResponse . status == 404 ) {
486
+ throw new error . UnsupportedOperationError ( value ) ;
487
+ } else if ( httpResponse . status >= 400 ) {
488
+ throw new error . WebDriverError ( value ) ;
466
489
}
467
490
468
- return response ;
491
+ return value ;
469
492
}
470
493
471
494
0 commit comments