(url, method, body = null)
| 263 | } |
| 264 | |
| 265 | async command (url, method, body = null) { |
| 266 | let response; |
| 267 | let resBodyObj; |
| 268 | try { |
| 269 | [response, resBodyObj] = await this.proxyCommand(url, method, body); |
| 270 | } catch (err) { |
| 271 | if (isErrorType(err, errors.ProxyRequestError)) { |
| 272 | throw err.getActualError(); |
| 273 | } |
| 274 | throw new errors.UnknownError(err.message); |
| 275 | } |
| 276 | const protocol = this.getProtocolFromResBody(resBodyObj); |
| 277 | if (protocol === MJSONWP) { |
| 278 | // Got response in MJSONWP format |
| 279 | if (response.statusCode === 200 && resBodyObj.status === 0) { |
| 280 | return resBodyObj.value; |
| 281 | } |
| 282 | const status = parseInt(resBodyObj.status, 10); |
| 283 | if (!isNaN(status) && status !== 0) { |
| 284 | let message = resBodyObj.value; |
| 285 | if (_.has(message, 'message')) { |
| 286 | message = message.message; |
| 287 | } |
| 288 | throw errorFromMJSONWPStatusCode(status, _.isEmpty(message) ? getSummaryByCode(status) : message); |
| 289 | } |
| 290 | } else if (protocol === W3C) { |
| 291 | // Got response in W3C format |
| 292 | if (response.statusCode < 300) { |
| 293 | return resBodyObj.value; |
| 294 | } |
| 295 | if (_.isPlainObject(resBodyObj.value) && resBodyObj.value.error) { |
| 296 | throw errorFromW3CJsonCode(resBodyObj.value.error, resBodyObj.value.message, resBodyObj.value.stacktrace); |
| 297 | } |
| 298 | } else if (response.statusCode === 200) { |
| 299 | // Unknown protocol. Keeping it because of the backward compatibility |
| 300 | return resBodyObj; |
| 301 | } |
| 302 | throw new errors.UnknownError(`Did not know what to do with response code '${response.statusCode}' ` + |
| 303 | `and response body '${_.truncate(JSON.stringify(resBodyObj), {length: 300})}'`); |
| 304 | } |
| 305 | |
| 306 | getSessionIdFromUrl (url) { |
| 307 | const match = url.match(/\/session\/([^/]+)/); |
no test coverage detected