@samet-it/be-base-common 1.0.9 → 1.0.11

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.
@@ -36,6 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.errorHandler = void 0;
37
37
  const stackTraceParser = __importStar(require("stacktrace-parser"));
38
38
  const base_exception_1 = require("./base.exception");
39
+ const common_1 = require("@leyyo/common");
39
40
  const KNOWN_GROUPS = {
40
41
  '@samet-digital': 's',
41
42
  '@leyyo': 'l',
@@ -43,7 +44,13 @@ const KNOWN_GROUPS = {
43
44
  };
44
45
  const IGNORED_KEYS = ['constructor', 'prototype', 'message', 'stack', 'name'];
45
46
  class ErrorHandler {
46
- item(value, set) {
47
+ constructor() {
48
+ this._CLEAR_STAT = 1000 * 60 * 60 * 24; // daily
49
+ this._stats = new Map();
50
+ this.started = new Date();
51
+ this._clearStats();
52
+ }
53
+ _item(value, set) {
47
54
  var _a;
48
55
  if (value === null || value === undefined) {
49
56
  return undefined;
@@ -61,11 +68,11 @@ class ErrorHandler {
61
68
  set.add(value);
62
69
  if (Array.isArray(value)) {
63
70
  return value
64
- .map(item => this.item(item, set));
71
+ .map(item => this._item(item, set));
65
72
  }
66
73
  else if (value instanceof Set) {
67
74
  return Array.from(value.values())
68
- .map(item => this.item(item, set));
75
+ .map(item => this._item(item, set));
69
76
  }
70
77
  else if (value instanceof Map) {
71
78
  const result = {};
@@ -73,7 +80,7 @@ class ErrorHandler {
73
80
  if (typeof k === 'symbol') {
74
81
  continue;
75
82
  }
76
- result[k] = this.item(v, set);
83
+ result[k] = this._item(v, set);
77
84
  }
78
85
  return result;
79
86
  }
@@ -82,7 +89,7 @@ class ErrorHandler {
82
89
  if (typeof k === 'symbol') {
83
90
  continue;
84
91
  }
85
- result[k] = this.item(v, set);
92
+ result[k] = this._item(v, set);
86
93
  }
87
94
  return result;
88
95
  }
@@ -93,10 +100,17 @@ class ErrorHandler {
93
100
  }
94
101
  return undefined;
95
102
  }
103
+ /**
104
+ * Clear native errors
105
+ * */
106
+ _clearStats() {
107
+ this._stats.clear();
108
+ setTimeout(() => this._clearStats(), this._CLEAR_STAT);
109
+ }
96
110
  checkParams(exception) {
97
111
  const set = new WeakSet();
98
112
  set.add(exception);
99
- exception.params = this.item(exception.params, set);
113
+ exception.params = this._item(exception.params, set);
100
114
  }
101
115
  export(exception) {
102
116
  var _a;
@@ -213,6 +227,43 @@ class ErrorHandler {
213
227
  // none
214
228
  }
215
229
  }
230
+ addStat(error) {
231
+ try {
232
+ if (!common_1.$is.object(error)) {
233
+ return;
234
+ }
235
+ const num = this._stats.get(error);
236
+ if (num === undefined) {
237
+ this._stats.set(error, 1);
238
+ }
239
+ else {
240
+ this._stats.set(error, num + 1);
241
+ }
242
+ }
243
+ catch (e) {
244
+ }
245
+ }
246
+ listStats() {
247
+ var _a, _b;
248
+ const map = {};
249
+ let id = 0;
250
+ for (const [error, num] of this._stats.entries()) {
251
+ let name;
252
+ try {
253
+ name = (_a = error.name) !== null && _a !== void 0 ? _a : (_b = error.constructor) === null || _b === void 0 ? void 0 : _b.name;
254
+ }
255
+ catch (e) {
256
+ id++;
257
+ name = `Error[${id}]`;
258
+ }
259
+ if (!name) {
260
+ id++;
261
+ name = `Error[${id}]`;
262
+ }
263
+ map[name] = num;
264
+ }
265
+ return map;
266
+ }
216
267
  }
217
268
  // noinspection JSUnusedGlobalSymbols
218
269
  exports.errorHandler = new ErrorHandler();
@@ -23,9 +23,12 @@ export interface ExceptionExported {
23
23
  }
24
24
  export type ErrorItem<A, C = ExceptionLike> = A extends ClassLike<infer R> ? (R extends ExceptionLike ? C : never) : never;
25
25
  export interface ErrorHandlerLike {
26
+ readonly started: Date;
26
27
  checkParams(exception: ExceptionLike): void;
27
28
  export(exception: ExceptionLike): ExceptionExported;
28
29
  cast<F extends ClassLike<ExceptionLike>>(error: Error, fnc?: F): ErrorItem<F>;
29
30
  cast<F extends ClassLike<ExceptionLike>>(error: Error, status: number, fnc?: F): ErrorItem<F>;
30
31
  stack(source: ExceptionLike, causedBy?: Error): void;
32
+ addStat(error: Error): void;
33
+ listStats(): Record<string, number>;
31
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-base-common",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Backend Base Common",
5
5
  "repository": {
6
6
  "type": "git",