* Callback used to parse Response objects from a * HttpClient. * * @param {!cmd.Command} command The command the response is for. * @param {!Response} httpResponse The HTTP response to parse. * @return {{isW3C: boolean, value: ?}} An object describing the parsed * response.
(command, httpResponse)
| 504 | * @throws {WebDriverError} If the HTTP response is an error. |
| 505 | */ |
| 506 | function parseHttpResponse(command, httpResponse) { |
| 507 | if (httpResponse.status < 200) { |
| 508 | // This should never happen, but throw the raw response so users report it. |
| 509 | throw new error.WebDriverError(`Unexpected HTTP response:\n${httpResponse}`) |
| 510 | } |
| 511 | |
| 512 | let parsed = tryParse(httpResponse.body) |
| 513 | |
| 514 | if (parsed && typeof parsed === 'object') { |
| 515 | let value = parsed.value |
| 516 | let isW3C = isObject(value) && typeof parsed.status === 'undefined' |
| 517 | |
| 518 | if (!isW3C) { |
| 519 | error.checkLegacyResponse(parsed) |
| 520 | |
| 521 | // Adjust legacy new session responses to look like W3C to simplify |
| 522 | // later processing. |
| 523 | if (command.getName() === cmd.Name.NEW_SESSION) { |
| 524 | value = parsed |
| 525 | } |
| 526 | } else if (httpResponse.status > 399) { |
| 527 | error.throwDecodedError(value) |
| 528 | } |
| 529 | |
| 530 | return { isW3C, value } |
| 531 | } |
| 532 | |
| 533 | if (parsed !== undefined) { |
| 534 | return { isW3C: false, value: parsed } |
| 535 | } |
| 536 | |
| 537 | let value = httpResponse.body.replace(/\r\n/g, '\n') |
| 538 | |
| 539 | // 404 represents an unknown command; anything else > 399 is a generic unknown |
| 540 | // error. |
| 541 | if (httpResponse.status === 404) { |
| 542 | throw new error.UnsupportedOperationError(command.getName() + ': ' + value) |
| 543 | } else if (httpResponse.status >= 400) { |
| 544 | throw new error.WebDriverError(value) |
| 545 | } |
| 546 | |
| 547 | return { isW3C: false, value: value || null } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Builds a fully qualified path using the given set of command parameters. Each |