report-error-server.js 1.13 KB
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.NO_APM_REPORT) {
      return;
    }

    if (process.env.NODE_ENV === 'production') {
      setImmediate(() => {
        const reportData = {
          measurement: 'error-report',
          tags: {
            app: 'ufo-app-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);
        }
      });
    }
  };
};