lrf 2 éve
szülő
commit
d697f663cf
3 módosított fájl, 53 hozzáadás és 2 törlés
  1. 28 0
      app/middleware/errorEmail.js
  2. 23 0
      app/service/util/email.js
  3. 2 2
      config/config.default.js

+ 28 - 0
app/middleware/errorEmail.js

@@ -0,0 +1,28 @@
+'use strict';
+const _ = require('lodash');
+module.exports = options => {
+  return async function errorEmail(ctx, next) {
+    console.log('line 5 in function:');
+    try {
+      await next();
+
+    } catch (e) {
+      let errcode = -1,
+        errmsg = '服务发生错误',
+        details;
+      const emailData = {};
+      if (_.isNumber(e.errcode)) {
+        errcode = _.get(e, 'errcode');
+        errmsg = _.get(e, 'errmsg');
+        details = _.get(e, 'details');
+        emailData.errmsg = errmsg;
+        emailData.details = details;
+      } else {
+        emailData.errmsg = e.stack;
+      }
+      emailData.errcode = errcode;
+      ctx.service.util.email.errorEmail(emailData);
+      ctx.fail(errcode, errmsg, details);
+    }
+  };
+};

+ 23 - 0
app/service/util/email.js

@@ -0,0 +1,23 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class EmailService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'email');
+    this.httpUtil = this.ctx.service.util.httpUtil;
+    this.emailServiceUrl = _.get(this.app, 'config.httpPrefix.email');
+    this.emailServiceConfig = _.get(this.app, 'config.emailConfig.config');
+  }
+
+  async errorEmail(error) {
+    const data = { config: this.emailServiceConfig, params: error };
+    const url = `${this.emailServiceUrl}/error`;
+    await this.httpUtil.cpost(url, data);
+  }
+}
+
+module.exports = EmailService;

+ 2 - 2
config/config.default.js

@@ -15,9 +15,9 @@ module.exports = appInfo => {
 
   // use for cookie sign key, should change to your own and keep security
   config.keys = appInfo.name + '_1664237342649_2194';
-
+  config.appName = '天恩活泉商城-服务';
   // add your middleware config here
-  config.middleware = [ 'setUserFromToken', 'checkLogin' ]; // , 'checkLogin'
+  config.middleware = [ 'errorEmail', 'setUserFromToken', 'checkLogin' ]; // , 'checkLogin'
 
   // add your user config here
   const userConfig = {