all files / raven-node/lib/ parsers.js

100% Statements 57/57
92.54% Branches 62/67
100% Functions 4/4
100% Lines 57/57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182              22× 22× 22× 22×   22×                 22× 22×       22×     22× 22× 22× 22×       22×       27×               27×               27×               27×               27×               27×     27×               27×               27×               27× 27×       27×                   27×     27×                   27×           27× 27× 26×             26×     26×     27×    
'use strict';
 
var cookie = require('cookie');
var urlParser = require('url');
var stringify = require('json-stringify-safe');
 
var utils = require('./utils');
 
module.exports.parseText = function parseText(message, kwargs) {
  kwargs = kwargs || {};
  kwargs.message = message;
 
  return kwargs;
};
 
module.exports.parseError = function parseError(err, kwargs, cb) {
  utils.parseStack(err, function (frames) {
    var name = err.name + '';
    Eif (typeof kwargs.message === 'undefined') {
      kwargs.message = name + ': ' + (err.message || '<no message>');
    }
    kwargs.exception = [{
      type: name,
      value: err.message,
      stacktrace: {
        frames: frames
      }
    }];
 
    // Save additional error properties to `extra` under the error type (e.g. `extra.AttributeError`)
    var extraErrorProps;
    for (var key in err) {
      Eif (err.hasOwnProperty(key)) {
        if (key !== 'name' && key !== 'message' && key !== 'stack') {
          extraErrorProps = extraErrorProps || {};
          extraErrorProps[key] = err[key];
        }
      }
    }
    if (extraErrorProps) {
      kwargs.extra = kwargs.extra || {};
      kwargs.extra[name] = extraErrorProps;
    }
 
    for (var n = frames.length - 1; n >= 0; n--) {
      Eif (frames[n].in_app) {
        kwargs.culprit = kwargs.culprit || utils.getCulprit(frames[n]);
        break;
      }
    }
 
    cb(kwargs);
  });
};
 
module.exports.parseRequest = function parseRequest(req, kwargs) {
  kwargs = kwargs || {};
 
  // headers:
  //
  //   node: req.headers
  //   express: req.headers
  //   koa: req.header
  //
  var headers = req.headers || req.header || {};
 
  // method:
  //
  //   node: req.method
  //   express: req.method
  //   koa: req.method
  //
  var method = req.method;
 
  // host:
  //
  //   node: req.headers.host
  //   express: req.hostname in > 4 and req.host in < 4
  //   koa: req.host
  //
  var host = req.hostname || req.host || headers.host || '<no host>';
 
  // protocol:
  //
  //   node: <n/a>
  //   express: req.protocol
  //   koa: req.protocol
  //
  var protocol = req.protocol === 'https' || req.secure || (req.socket || {}).encrypted ? 'https' : 'http';
 
  // url (including path and query string):
  //
  //   node: req.originalUrl
  //   express: req.originalUrl
  //   koa: req.url
  //
  var originalUrl = req.originalUrl || req.url;
 
  // absolute url
  var url = protocol + '://' + host + originalUrl;
 
  // query string
  //
  //   node: req.url (raw)
  //   express: req.query
  //   koa: req.query
  //
  var query = req.query || urlParser.parse(originalUrl || '', true).query;
 
  // cookies:
  //
  //   node: req.headers.cookie
  //   express: req.headers.cookie
  //   koa: req.headers.cookie
  //
  var cookies = cookie.parse(headers.cookie || '');
 
  // body data:
  //
  //   node: req.body
  //   express: req.body
  //   koa: req.body
  //
  var data = req.body;
  if (['GET', 'HEAD'].indexOf(method) === -1) {
    if (typeof data === 'undefined') {
      data = '<unavailable>';
    }
  }
 
  if (data && {}.toString.call(data) !== '[object String]') {
    // Make sure the request body is a string
    data = stringify(data);
  }
 
  // client ip:
  //
  //   node: req.connection.remoteAddress
  //   express: req.ip
  //   koa: req.ip
  //
  var ip = req.ip || (req.connection || {}).remoteAddress;
 
  // http interface
  var http = {
    method: method,
    query_string: query,
    headers: headers,
    cookies: cookies,
    data: data,
    url: url
  };
 
  // expose http interface
  kwargs.request = http;
 
  // user
  //
  // typically found on req.user according to Express and Passport
 
  var user = {};
  if (!kwargs.user) {
    if (req.user) {
      // shallow copy is okay because we are only modifying top-level
      // object (req.user)
      for (var key in req.user) {
        Eif ({}.hasOwnProperty.call(req.user, key)) {
          user[key] = req.user[key];
        }
      }
    }
 
    if (ip) {
      user.ip_address = ip;
    }
 
    kwargs.user = user;
  }
 
  return kwargs;
};