@leyyo/common 1.3.12 → 1.3.14
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.
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { ErrorCommonLike, ErrorItemConfig, ErrorObject } from "./index.types.js";
|
|
1
|
+
import { ErrorCommonLike, ErrorCtor, ErrorItemConfig, ErrorObject } from "./index.types.js";
|
|
2
2
|
import { ClassLike, LeyyoLike } from "../base/index.js";
|
|
3
3
|
import { Opt } from "../function/index.js";
|
|
4
4
|
import { LeyyoErrorLike } from "../error/index.js";
|
|
5
5
|
export declare class ErrorCommon implements ErrorCommonLike {
|
|
6
6
|
private leyyo;
|
|
7
7
|
private _knownPackages;
|
|
8
|
+
private _stats;
|
|
9
|
+
readonly started: Date;
|
|
8
10
|
constructor(leyyo: LeyyoLike);
|
|
9
11
|
/**
|
|
10
12
|
* Transform error as a bare object
|
|
@@ -45,4 +47,12 @@ export declare class ErrorCommon implements ErrorCommonLike {
|
|
|
45
47
|
bareObj(err: Error): ErrorObject;
|
|
46
48
|
/** @inheritDoc */
|
|
47
49
|
text(err: Error, ...parts: Array<string | number>): string;
|
|
50
|
+
/** @inheritDoc */
|
|
51
|
+
addStat(p1: Error | ErrorCtor): number;
|
|
52
|
+
/** @inheritDoc */
|
|
53
|
+
getStat(p1: Error | ErrorCtor): number;
|
|
54
|
+
/** @inheritDoc */
|
|
55
|
+
clearStats(): void;
|
|
56
|
+
/** @inheritDoc */
|
|
57
|
+
listStats(): Record<string, number>;
|
|
48
58
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { getSymbol, isClass, isEmpty, isFilledArr, isFilledObj, isObj, isText, secureJson, setSymbol, testCase } from "../function/index.js";
|
|
1
|
+
import { getFqn, getSymbol, isClass, isEmpty, isFilledArr, isFilledObj, isObj, isText, secureJson, setSymbol, testCase } from "../function/index.js";
|
|
2
2
|
import * as stackTraceParser from "stacktrace-parser";
|
|
3
3
|
import { FQN } from "../internal.js";
|
|
4
|
-
import { KEY_ERROR_DEFAULT_MESSAGE, KEY_ERROR_EMIT, KEY_ERROR_EMITTED, KEY_ERROR_I18N, VAL_ERROR_UNKNOWN_MESSAGE, VAL_ERROR_UNKNOWN_NAME } from "../const/index.js";
|
|
4
|
+
import { KEY_ERROR_DEFAULT_MESSAGE, KEY_ERROR_EMIT, KEY_ERROR_EMITTED, KEY_ERROR_I18N, KEY_ERROR_RAISED, VAL_ERROR_UNKNOWN_MESSAGE, VAL_ERROR_UNKNOWN_NAME } from "../const/index.js";
|
|
5
5
|
const where = `${FQN}.ErrorCommon`;
|
|
6
6
|
// noinspection JSUnusedGlobalSymbols
|
|
7
7
|
export class ErrorCommon {
|
|
8
8
|
leyyo;
|
|
9
9
|
_knownPackages;
|
|
10
|
+
_stats;
|
|
11
|
+
started;
|
|
10
12
|
constructor(leyyo) {
|
|
11
13
|
this.leyyo = leyyo;
|
|
12
14
|
}
|
|
@@ -285,4 +287,81 @@ export class ErrorCommon {
|
|
|
285
287
|
}
|
|
286
288
|
return `${info}[err:${VAL_ERROR_UNKNOWN_NAME}] => ^/${VAL_ERROR_UNKNOWN_MESSAGE}/$`;
|
|
287
289
|
}
|
|
290
|
+
/** @inheritDoc */
|
|
291
|
+
addStat(p1) {
|
|
292
|
+
let num = -1;
|
|
293
|
+
try {
|
|
294
|
+
let clazz;
|
|
295
|
+
if (isObj(p1)) {
|
|
296
|
+
const err = p1;
|
|
297
|
+
if (err[KEY_ERROR_RAISED]) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
err[KEY_ERROR_RAISED] = true;
|
|
301
|
+
clazz = err.constructor;
|
|
302
|
+
}
|
|
303
|
+
else if (typeof p1 === 'function') {
|
|
304
|
+
clazz = p1;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (!this._stats) {
|
|
310
|
+
this._stats = this.leyyo.repoCommon.newMap(`${FQN}.error`);
|
|
311
|
+
}
|
|
312
|
+
num = this._stats.get(clazz);
|
|
313
|
+
if (num === undefined) {
|
|
314
|
+
num = 1;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
num++;
|
|
318
|
+
}
|
|
319
|
+
this._stats.set(clazz, num);
|
|
320
|
+
}
|
|
321
|
+
catch (e) {
|
|
322
|
+
// nothing
|
|
323
|
+
}
|
|
324
|
+
return num;
|
|
325
|
+
}
|
|
326
|
+
/** @inheritDoc */
|
|
327
|
+
getStat(p1) {
|
|
328
|
+
try {
|
|
329
|
+
let clazz;
|
|
330
|
+
if (isObj(p1)) {
|
|
331
|
+
clazz = p1.constructor;
|
|
332
|
+
}
|
|
333
|
+
else if (typeof p1 === 'function') {
|
|
334
|
+
clazz = p1;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
return 0;
|
|
338
|
+
}
|
|
339
|
+
if (!this._stats) {
|
|
340
|
+
return 0;
|
|
341
|
+
}
|
|
342
|
+
return this._stats.get(clazz) ?? 0;
|
|
343
|
+
}
|
|
344
|
+
catch (e) {
|
|
345
|
+
// nothing
|
|
346
|
+
}
|
|
347
|
+
return 0;
|
|
348
|
+
}
|
|
349
|
+
/** @inheritDoc */
|
|
350
|
+
clearStats() {
|
|
351
|
+
if (!this._stats) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this._stats.clear();
|
|
355
|
+
}
|
|
356
|
+
/** @inheritDoc */
|
|
357
|
+
listStats() {
|
|
358
|
+
if (!this._stats) {
|
|
359
|
+
return {};
|
|
360
|
+
}
|
|
361
|
+
const map = {};
|
|
362
|
+
for (const [error, num] of this._stats.entries()) {
|
|
363
|
+
map[getFqn(error)] = num;
|
|
364
|
+
}
|
|
365
|
+
return map;
|
|
366
|
+
}
|
|
288
367
|
}
|
|
@@ -160,6 +160,9 @@ export interface ErrorPoolLike extends InertLike<InertItem<ClassLike>, ClassLike
|
|
|
160
160
|
* Bare omit error without any property
|
|
161
161
|
* */
|
|
162
162
|
export type OmitError = Omit<Error, 'name' | 'message' | 'stack'>;
|
|
163
|
+
export interface ErrorCtor extends Fnc {
|
|
164
|
+
new (...args: Array<unknown>): OmitError;
|
|
165
|
+
}
|
|
163
166
|
export interface ErrorObject {
|
|
164
167
|
name: string;
|
|
165
168
|
message: string;
|
|
@@ -242,6 +245,44 @@ export interface ErrorCommonLike {
|
|
|
242
245
|
* @return {string} - error text
|
|
243
246
|
* */
|
|
244
247
|
text(err: Error, ...parts: Array<string | number>): string;
|
|
248
|
+
/**
|
|
249
|
+
* Add error statistics with instance
|
|
250
|
+
*
|
|
251
|
+
* @param {Error} error
|
|
252
|
+
* @return {number} - total raised count
|
|
253
|
+
* */
|
|
254
|
+
addStat(error: Error): number;
|
|
255
|
+
/**
|
|
256
|
+
* Add error statistics
|
|
257
|
+
*
|
|
258
|
+
* @param {ErrorCtor} clazz
|
|
259
|
+
* @return {number} - total raised count
|
|
260
|
+
* */
|
|
261
|
+
addStat(clazz: ErrorCtor): number;
|
|
262
|
+
/**
|
|
263
|
+
* Get error statistics with instance
|
|
264
|
+
*
|
|
265
|
+
* @param {Error} error
|
|
266
|
+
* @return {number} - total raised count
|
|
267
|
+
* */
|
|
268
|
+
getStat(error: Error): number;
|
|
269
|
+
/**
|
|
270
|
+
* Get error statistics
|
|
271
|
+
*
|
|
272
|
+
* @param {ErrorCtor} clazz
|
|
273
|
+
* @return {number} - total raised count
|
|
274
|
+
* */
|
|
275
|
+
getStat(clazz: ErrorCtor): number;
|
|
276
|
+
/**
|
|
277
|
+
* Clear statistics
|
|
278
|
+
* */
|
|
279
|
+
clearStats(): void;
|
|
280
|
+
/**
|
|
281
|
+
* List statistics
|
|
282
|
+
*
|
|
283
|
+
* @return {Record} - as {[error-name]: number}
|
|
284
|
+
* */
|
|
285
|
+
listStats(): Record<string, number>;
|
|
245
286
|
}
|
|
246
287
|
export type Enum<E extends KeyValue = KeyValue> = {
|
|
247
288
|
[K in E]: KeyValue;
|
package/dist/const/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare const VAL_ERROR_UNKNOWN_MESSAGE: string;
|
|
|
17
17
|
export declare const KEY_ERROR_DEFAULT_MESSAGE: unique symbol;
|
|
18
18
|
export declare const KEY_ERROR_I18N: unique symbol;
|
|
19
19
|
export declare const KEY_ERROR_EMIT: unique symbol;
|
|
20
|
+
export declare const KEY_ERROR_RAISED: unique symbol;
|
|
20
21
|
export declare const KEY_ERROR_EMITTED: unique symbol;
|
|
21
22
|
export declare const KEY_ERROR_FLAGS: unique symbol;
|
|
22
23
|
export declare const KEY_ERROR_WHERE: unique symbol;
|
package/dist/const/index.js
CHANGED
|
@@ -18,7 +18,8 @@ export const VAL_ERROR_UNKNOWN_MESSAGE = 'Unknown error';
|
|
|
18
18
|
export const KEY_ERROR_DEFAULT_MESSAGE = Symbol.for('leyyo:error:message');
|
|
19
19
|
export const KEY_ERROR_I18N = Symbol.for('leyyo:error:i18n');
|
|
20
20
|
export const KEY_ERROR_EMIT = Symbol.for('leyyo:error:emit');
|
|
21
|
-
export const
|
|
21
|
+
export const KEY_ERROR_RAISED = Symbol.for('leyyo:error:raised');
|
|
22
|
+
export const KEY_ERROR_EMITTED = Symbol.for('leyyo:error:emitted');
|
|
22
23
|
export const KEY_ERROR_FLAGS = Symbol.for('leyyo:error:flags');
|
|
23
24
|
export const KEY_ERROR_WHERE = Symbol.for('leyyo:error:where');
|
|
24
25
|
export const KEY_LOADER_NAME = Symbol.for('leyyo:loader:name');
|