mm_machine 3.1.6 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/drive.js CHANGED
@@ -94,7 +94,7 @@ Drive.prototype._loadConfig = function (file) {
94
94
  this.setConfig(config);
95
95
  return config;
96
96
  } catch (err) {
97
- this.log('error', '加载配置失败', err);
97
+ this.log('error', '加载配置失败', this._wrapError(err));
98
98
  return null;
99
99
  }
100
100
  };
@@ -333,7 +333,7 @@ Drive.prototype.loadScript = function () {
333
333
  return cs;
334
334
  }
335
335
  } catch (err) {
336
- this.log('error', '加载脚本文件失败', err);
336
+ this.log('error', '加载脚本文件失败', this._wrapError(err));
337
337
  return null;
338
338
  }
339
339
  };
@@ -668,7 +668,7 @@ Drive.prototype.loadBefore = function () {
668
668
  this._is_loaded = true;
669
669
  }
670
670
  } catch (err) {
671
- this.log('error', `加载前处理失败`, err);
671
+ this.log('error', `加载前处理失败`, this._wrapError(err));
672
672
  this._is_loaded = false;
673
673
  }
674
674
  };
package/mod.js CHANGED
@@ -113,6 +113,39 @@ Mod.prototype.log = function (level, message, ...params) {
113
113
  this.getLogger()[level](`[${this.constructor.name}.${this.config.name}] ${message}`, ...params);
114
114
  };
115
115
 
116
+ /**
117
+ * 包装错误,附加模块文件路径上下文,用于溯源
118
+ * @param {Error} err 原始错误对象
119
+ * @param {object} context 上下文信息
120
+ * @param {string} context.method 方法名
121
+ * @returns {Error} 包装后的错误
122
+ */
123
+ Mod.prototype._wrapError = function (err, context = {}) {
124
+ // 已包装过的不再重复包装
125
+ if (err.__wrapped) return err;
126
+
127
+ var parts = [];
128
+ // 附加配置文件路径
129
+ if (this.config_file) {
130
+ parts.push('cfg:' + this.config_file);
131
+ }
132
+ // 附加脚本文件路径
133
+ if (this.script_file) {
134
+ parts.push('script:' + this.script_file);
135
+ }
136
+ // 附加方法名
137
+ if (context.method) {
138
+ parts.push(context.method);
139
+ }
140
+ var prefix = '[' + this.constructor.name + '.' + this.config.name + ']';
141
+ var suffix = parts.length > 0 ? ' (' + parts.join(', ') + ')' : '';
142
+ var msg = prefix + suffix + ' - ' + err.message;
143
+ var wrapped = new Error(msg);
144
+ wrapped.stack = wrapped.stack + '\n' + err.stack;
145
+ wrapped.__wrapped = true;
146
+ return wrapped;
147
+ };
148
+
116
149
  /**
117
150
  * 监听事件
118
151
  */
@@ -649,7 +682,7 @@ Mod.prototype.call = async function (method, ...params) {
649
682
  result = await this._callMain(method_func, params, before_result);
650
683
  result = await this._callAfter(method, params, result);
651
684
  } catch (err) {
652
- this.log('error', `执行 ${method} 失败: ${err.message}`, err);
685
+ this.log('error', `执行 ${method} 失败: ${err.message}`, this._wrapError(err, { method: method }));
653
686
  return null;
654
687
  }
655
688
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.1.6",
3
+ "version": "3.1.8",
4
4
  "description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/registry.js CHANGED
@@ -12,7 +12,7 @@ class Registry {
12
12
  constructor(config) {
13
13
  this.config = {
14
14
  name: 'default',
15
- dir: '/data/registry'.fullname(__dirname),
15
+ dir: '/data/registry'.fullname(),
16
16
  ...config
17
17
  };
18
18
  this.mod_dict = {};
@@ -260,11 +260,6 @@ Registry.prototype.sort = function () {
260
260
  }
261
261
  };
262
262
 
263
-
264
- if (!$.registry) {
265
- $.registry = new Registry({ name: 'default' });
266
- }
267
-
268
263
  module.exports = {
269
264
  Registry
270
265
  };