Example #1
0
 async command (url, method, body = null) {
   let [response, resBody] = await this.proxy(url, method, body);
   let statusCodesWithRes = [100, 200, 500];
   resBody = util.safeJsonParse(resBody);
   if (_.includes(statusCodesWithRes, response.statusCode) &&
       (_.isUndefined(resBody.status) || _.isUndefined(resBody.value))) {
     throw new Error(`Did not get a valid response object. Object was: ${JSON.stringify(resBody)}`);
   }
   if (_.includes(statusCodesWithRes, response.statusCode)) {
     if (response.statusCode === 200 && resBody.status === 0) {
       return resBody.value;
     } else if (response.statusCode === 200) {
       return resBody;
     }
     let message = getSummaryByCode(resBody.status);
     if (resBody.value.message) {
       message += ` (Original error: ${resBody.value.message})`;
     }
     let e = new Error(message);
     e.status = resBody.status;
     e.value = resBody.value;
     e.httpCode = response.statusCode;
     throw e;
   }
   throw new Error(`Didn't know what to do with response code '${response.statusCode}'`);
 }
Example #2
0
 async proxyReqRes (req, res) {
   let [response, body] = await this.proxy(req.originalUrl, req.method, req.body);
   res.headers = response.headers;
   res.set('Content-type', response.headers['content-type']);
   // if the proxied response contains a sessionId that the downstream
   // driver has generated, we don't want to return that to the client.
   // Instead, return the id from the request or from current session
   body = util.safeJsonParse(body);
   if (body && body.sessionId) {
     const reqSessionId = this.getSessionIdFromUrl(req.originalUrl);
     if (reqSessionId) {
       log.info(`Replacing sessionId ${body.sessionId} with ${reqSessionId}`);
       body.sessionId = reqSessionId;
     } else if (this.sessionId) {
       log.info(`Replacing sessionId ${body.sessionId} with ${this.sessionId}`);
       body.sessionId = this.sessionId;
     }
   }
   res.status(response.statusCode).send(JSON.stringify(body));
 }