@samet-it/be-base-common 1.0.9 → 1.0.10
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,15 @@ const KNOWN_GROUPS = {
|
|
|
43
44
|
};
|
|
44
45
|
const IGNORED_KEYS = ['constructor', 'prototype', 'message', 'stack', 'name'];
|
|
45
46
|
class ErrorHandler {
|
|
46
|
-
|
|
47
|
+
constructor() {
|
|
48
|
+
this._CLEAR_STAT = 1000 * 60 * 60 * 24; // daily
|
|
49
|
+
this._stats = new Map();
|
|
50
|
+
const now = new Date();
|
|
51
|
+
this.started = now.getTime();
|
|
52
|
+
this.startedIso = now.toISOString();
|
|
53
|
+
this._clearStats();
|
|
54
|
+
}
|
|
55
|
+
_item(value, set) {
|
|
47
56
|
var _a;
|
|
48
57
|
if (value === null || value === undefined) {
|
|
49
58
|
return undefined;
|
|
@@ -61,11 +70,11 @@ class ErrorHandler {
|
|
|
61
70
|
set.add(value);
|
|
62
71
|
if (Array.isArray(value)) {
|
|
63
72
|
return value
|
|
64
|
-
.map(item => this.
|
|
73
|
+
.map(item => this._item(item, set));
|
|
65
74
|
}
|
|
66
75
|
else if (value instanceof Set) {
|
|
67
76
|
return Array.from(value.values())
|
|
68
|
-
.map(item => this.
|
|
77
|
+
.map(item => this._item(item, set));
|
|
69
78
|
}
|
|
70
79
|
else if (value instanceof Map) {
|
|
71
80
|
const result = {};
|
|
@@ -73,7 +82,7 @@ class ErrorHandler {
|
|
|
73
82
|
if (typeof k === 'symbol') {
|
|
74
83
|
continue;
|
|
75
84
|
}
|
|
76
|
-
result[k] = this.
|
|
85
|
+
result[k] = this._item(v, set);
|
|
77
86
|
}
|
|
78
87
|
return result;
|
|
79
88
|
}
|
|
@@ -82,7 +91,7 @@ class ErrorHandler {
|
|
|
82
91
|
if (typeof k === 'symbol') {
|
|
83
92
|
continue;
|
|
84
93
|
}
|
|
85
|
-
result[k] = this.
|
|
94
|
+
result[k] = this._item(v, set);
|
|
86
95
|
}
|
|
87
96
|
return result;
|
|
88
97
|
}
|
|
@@ -93,10 +102,17 @@ class ErrorHandler {
|
|
|
93
102
|
}
|
|
94
103
|
return undefined;
|
|
95
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Clear native errors
|
|
107
|
+
* */
|
|
108
|
+
_clearStats() {
|
|
109
|
+
this._stats.clear();
|
|
110
|
+
setTimeout(() => this._clearStats(), this._CLEAR_STAT);
|
|
111
|
+
}
|
|
96
112
|
checkParams(exception) {
|
|
97
113
|
const set = new WeakSet();
|
|
98
114
|
set.add(exception);
|
|
99
|
-
exception.params = this.
|
|
115
|
+
exception.params = this._item(exception.params, set);
|
|
100
116
|
}
|
|
101
117
|
export(exception) {
|
|
102
118
|
var _a;
|
|
@@ -213,6 +229,43 @@ class ErrorHandler {
|
|
|
213
229
|
// none
|
|
214
230
|
}
|
|
215
231
|
}
|
|
232
|
+
addStat(error) {
|
|
233
|
+
try {
|
|
234
|
+
if (!common_1.$is.object(error)) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const num = this._stats.get(error);
|
|
238
|
+
if (num === undefined) {
|
|
239
|
+
this._stats.set(error, 1);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this._stats.set(error, num + 1);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
listStats() {
|
|
249
|
+
var _a, _b;
|
|
250
|
+
const map = {};
|
|
251
|
+
let id = 0;
|
|
252
|
+
for (const [error, num] of this._stats.entries()) {
|
|
253
|
+
let name;
|
|
254
|
+
try {
|
|
255
|
+
name = (_a = error.name) !== null && _a !== void 0 ? _a : (_b = error.constructor) === null || _b === void 0 ? void 0 : _b.name;
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
id++;
|
|
259
|
+
name = `Error[${id}]`;
|
|
260
|
+
}
|
|
261
|
+
if (!name) {
|
|
262
|
+
id++;
|
|
263
|
+
name = `Error[${id}]`;
|
|
264
|
+
}
|
|
265
|
+
map[name] = num;
|
|
266
|
+
}
|
|
267
|
+
return map;
|
|
268
|
+
}
|
|
216
269
|
}
|
|
217
270
|
// noinspection JSUnusedGlobalSymbols
|
|
218
271
|
exports.errorHandler = new ErrorHandler();
|
|
@@ -23,9 +23,13 @@ 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: number;
|
|
27
|
+
readonly startedIso: string;
|
|
26
28
|
checkParams(exception: ExceptionLike): void;
|
|
27
29
|
export(exception: ExceptionLike): ExceptionExported;
|
|
28
30
|
cast<F extends ClassLike<ExceptionLike>>(error: Error, fnc?: F): ErrorItem<F>;
|
|
29
31
|
cast<F extends ClassLike<ExceptionLike>>(error: Error, status: number, fnc?: F): ErrorItem<F>;
|
|
30
32
|
stack(source: ExceptionLike, causedBy?: Error): void;
|
|
33
|
+
addStat(error: Error): void;
|
|
34
|
+
listStats(): Record<string, number>;
|
|
31
35
|
}
|