report-error-client.js 1.18 KB
import config from 'config';

const stringify = function(list) {
  let data = [];

  for (let i = 0; i < list.length; i++) {
    let obj = list[i];

    let params = [];

    for (let prop in obj) {
      if (obj.hasOwnProperty(prop)) {
        params.push(prop + '::' + obj[prop]);
      }
    }

    data.push(params.join('$$'));
  }

  return data.join('**');
};

const report = function(data) {
  const str = stringify([data]);

  if (!str) {
    return;
  }
  const imgElem = new Image();

  imgElem.src = config.reportUrl + '?s=yoho-community-web&l=' + str + '&t=' + (new Date()).getTime();
};

export default context => {
  return (err) => {
    if (process.env.NODE_ENV === 'production') {
      setTimeout(() => {
        try {
          report({
            tp: 'err',
            msg: err.message,
            sc: 'cdn.yoho.cn',
            ln: 0,
            cn: 0,
            pt: encodeURIComponent(location.href),
            u: 0,
            ud: 0,
            rid: 0,
            st: JSON.stringify(err && err.stack),
            r: context.route
          });
        } catch (error) {
          console.log(error);
        }
      }, 0);
    } else {
      console.log(err);
    }
  };
};