Blame view

apps/common/report-error-server.js 1.08 KB
陈峰 authored
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
import {get} from 'lodash';

const sender = global.yoho.apmSender;
const logger = global.yoho.logger;

export default (context, type = 'server') => {
  return (err, vm, info) => {
    logger.error(err, vm, info);

    if (process.env.NODE_ENV === 'production') {
      setImmediate(() => {
        const reportData = {
          measurement: 'error-report',
          tags: {
            app: 'yoho-community-web', // 应用名称
            hostname: context.hostname,
            type: type,
            route: context.route, // 请求路由
            uid: get(context, 'user.uid', 0),
            udid: context.udid,
            api: err.api,
            code: err.code || 500,
            path: context.path,
            url: encodeURIComponent(context.url),
            ip: context.env.clientIp
          },
          fields: {
            useragent: context.ua,
            message: err.message,
            stack: err.stack
          }
        };

        try {
          sender.addMessage(reportData);
        } catch (error) {
          logger.error(error);
        }
      });
    }
  };
};