@leyyo/common 1.3.12 → 1.3.13

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,10 @@ 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
+ clearStats(): void;
54
+ /** @inheritDoc */
55
+ listStats(): Record<string, number>;
48
56
  }
@@ -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,58 @@ 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
+ clearStats() {
328
+ if (!this._stats) {
329
+ return;
330
+ }
331
+ this._stats.clear();
332
+ }
333
+ /** @inheritDoc */
334
+ listStats() {
335
+ if (!this._stats) {
336
+ return {};
337
+ }
338
+ const map = {};
339
+ for (const [error, num] of this._stats.entries()) {
340
+ map[getFqn(error)] = num;
341
+ }
342
+ return map;
343
+ }
288
344
  }
@@ -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,30 @@ 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
+ * Clear statistics
264
+ * */
265
+ clearStats(): void;
266
+ /**
267
+ * List statistics
268
+ *
269
+ * @return {Record} - as {[error-name]: number}
270
+ * */
271
+ listStats(): Record<string, number>;
245
272
  }
246
273
  export type Enum<E extends KeyValue = KeyValue> = {
247
274
  [K in E]: KeyValue;
@@ -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;
@@ -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 KEY_ERROR_EMITTED = Symbol.for('leyyo:error:emit');
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');
@@ -42,6 +42,7 @@ export class LeyyoError extends Error {
42
42
  }
43
43
  super(message);
44
44
  const clazz = this.constructor;
45
+ _leyyo.errorCommon.addStat(this);
45
46
  if (!message) {
46
47
  const conf = _leyyo.errorCommon.getConfigItem(clazz);
47
48
  this.message = conf?.message;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leyyo/common",
3
- "version": "1.3.12",
3
+ "version": "1.3.13",
4
4
  "description": "Leyyo common library",
5
5
  "keywords": [
6
6
  "common"