@leyyo/common 1.0.0

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.
Files changed (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/dist/aliases.d.ts +139 -0
  4. package/dist/aliases.js +4 -0
  5. package/dist/assertion/assert.d.ts +6 -0
  6. package/dist/assertion/assert.js +13 -0
  7. package/dist/assertion/common-assertion-impl.d.ts +34 -0
  8. package/dist/assertion/common-assertion-impl.js +210 -0
  9. package/dist/assertion/index-types.d.ts +79 -0
  10. package/dist/assertion/index-types.js +2 -0
  11. package/dist/assertion/index.d.ts +3 -0
  12. package/dist/assertion/index.js +19 -0
  13. package/dist/callback/common-callback-impl.d.ts +31 -0
  14. package/dist/callback/common-callback-impl.js +134 -0
  15. package/dist/callback/index-types.d.ts +75 -0
  16. package/dist/callback/index-types.js +2 -0
  17. package/dist/callback/index.d.ts +2 -0
  18. package/dist/callback/index.js +18 -0
  19. package/dist/constants.d.ts +7 -0
  20. package/dist/constants.js +10 -0
  21. package/dist/error/assertion-exception.d.ts +5 -0
  22. package/dist/error/assertion-exception.js +11 -0
  23. package/dist/error/caused-exception.d.ts +5 -0
  24. package/dist/error/caused-exception.js +12 -0
  25. package/dist/error/common-error-impl.d.ts +24 -0
  26. package/dist/error/common-error-impl.js +88 -0
  27. package/dist/error/developer-exception.d.ts +5 -0
  28. package/dist/error/developer-exception.js +11 -0
  29. package/dist/error/exception.d.ts +33 -0
  30. package/dist/error/exception.js +124 -0
  31. package/dist/error/index-types.d.ts +70 -0
  32. package/dist/error/index-types.js +2 -0
  33. package/dist/error/index.d.ts +7 -0
  34. package/dist/error/index.js +23 -0
  35. package/dist/error/multiple-exception.d.ts +8 -0
  36. package/dist/error/multiple-exception.js +33 -0
  37. package/dist/index.d.ts +7 -0
  38. package/dist/index.js +26 -0
  39. package/dist/internal.d.ts +1 -0
  40. package/dist/internal.js +4 -0
  41. package/dist/is/common-is-impl.d.ts +22 -0
  42. package/dist/is/common-is-impl.js +66 -0
  43. package/dist/is/index-types.d.ts +22 -0
  44. package/dist/is/index-types.js +2 -0
  45. package/dist/is/index.d.ts +2 -0
  46. package/dist/is/index.js +18 -0
  47. package/dist/leyyo/index-types.d.ts +16 -0
  48. package/dist/leyyo/index-types.js +2 -0
  49. package/dist/leyyo/index.d.ts +2 -0
  50. package/dist/leyyo/index.js +18 -0
  51. package/dist/leyyo/leyyo-impl.d.ts +24 -0
  52. package/dist/leyyo/leyyo-impl.js +71 -0
  53. package/dist/literals.d.ts +98 -0
  54. package/dist/literals.js +77 -0
  55. package/dist/log/common-log-impl.d.ts +13 -0
  56. package/dist/log/common-log-impl.js +59 -0
  57. package/dist/log/index-types.d.ts +58 -0
  58. package/dist/log/index-types.js +3 -0
  59. package/dist/log/index.d.ts +3 -0
  60. package/dist/log/index.js +19 -0
  61. package/dist/log/logger-impl.d.ts +23 -0
  62. package/dist/log/logger-impl.js +73 -0
  63. package/dist/storage/common-storage-impl.d.ts +46 -0
  64. package/dist/storage/common-storage-impl.js +150 -0
  65. package/dist/storage/index-types.d.ts +102 -0
  66. package/dist/storage/index-types.js +2 -0
  67. package/dist/storage/index.d.ts +3 -0
  68. package/dist/storage/index.js +19 -0
  69. package/dist/storage/list.d.ts +9 -0
  70. package/dist/storage/list.js +16 -0
  71. package/dist/to/common-to-impl.d.ts +38 -0
  72. package/dist/to/common-to-impl.js +553 -0
  73. package/dist/to/index-types.d.ts +50 -0
  74. package/dist/to/index-types.js +2 -0
  75. package/dist/to/index.d.ts +2 -0
  76. package/dist/to/index.js +18 -0
  77. package/package.json +53 -0
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommonCallbackImpl = void 0;
4
+ const constants_1 = require("../constants");
5
+ const error_1 = require("../error");
6
+ const internal_1 = require("../internal");
7
+ class CommonCallbackImpl {
8
+ fqnName(value) {
9
+ switch (typeof value) {
10
+ case "function":
11
+ return value.name;
12
+ case "object":
13
+ return value.constructor.name;
14
+ case "string":
15
+ return value;
16
+ default:
17
+ return null;
18
+ }
19
+ }
20
+ /**
21
+ * Default constructor
22
+ *
23
+ * Responsibilities
24
+ * - Create repositories => ie: callbacks
25
+ * - Trigger clear pending operation
26
+ * */
27
+ constructor() {
28
+ // clear after 1 minute
29
+ setTimeout(() => this._clearPending(), 60000);
30
+ }
31
+ $init(leyyo) {
32
+ this._waitingForCallbacks = leyyo.storage.newMap(`${internal_1.FQN_PCK}/waitingForCallbacks`);
33
+ this._attachedCallbacks = leyyo.storage.newMap(`${internal_1.FQN_PCK}/attachedCallbacks`);
34
+ this._waitingForProviders = leyyo.storage.newMap(`${internal_1.FQN_PCK}/waitingForProviders`);
35
+ this._definedProviders = leyyo.storage.newMap(`${internal_1.FQN_PCK}/definedProviders`);
36
+ // define itself temporarily for fqn name
37
+ this.defineProvider(constants_1.LY_ATTACHED_FQN, CommonCallbackImpl, {
38
+ proper: false,
39
+ name: this.fqnName,
40
+ });
41
+ // when new fqn provider is defined, replace temporary name method
42
+ this.whenProviderDefined(constants_1.LY_ATTACHED_FQN, CommonCallbackImpl, (ins) => {
43
+ if (typeof ins.name === 'function') {
44
+ this.fqnName = ins.name;
45
+ }
46
+ });
47
+ }
48
+ /**
49
+ * Clear jobs in the queue which named as pending
50
+ *
51
+ * Because the expected callback may not be defined
52
+ * */
53
+ _clearPending() {
54
+ for (const [name,] of this._waitingForCallbacks.entries()) {
55
+ const rec = this._attachedCallbacks.get(name);
56
+ if (rec && !rec.initialization) {
57
+ continue;
58
+ }
59
+ this._waitingForCallbacks.delete(name);
60
+ console.log(`callback.cleared.pending => {name: ${name}}`);
61
+ }
62
+ }
63
+ get $secure() {
64
+ return this;
65
+ }
66
+ attachCallback(name, fn) {
67
+ if (!name || typeof name !== 'string') {
68
+ throw new Error(`callback.invalid.name => {type: ${typeof name}}`);
69
+ }
70
+ // callback attached
71
+ this._attachedCallbacks.set(name, { fn });
72
+ // check waiting records, to be called
73
+ if (this._waitingForCallbacks.has(name)) {
74
+ this._waitingForCallbacks.get(name).forEach(item => {
75
+ fn(...item);
76
+ });
77
+ this._waitingForCallbacks.delete(name);
78
+ }
79
+ }
80
+ queueForCallback(name, ...args) {
81
+ if (!name || typeof name !== 'string') {
82
+ throw new Error(`callback.invalid.name => {type: ${typeof name}}`);
83
+ }
84
+ // callback already exists
85
+ if (this._attachedCallbacks.has(name)) {
86
+ const rec = this._attachedCallbacks.get(name);
87
+ rec.fn(...args);
88
+ return true;
89
+ }
90
+ // callback does not exist yet, so wait to be attached
91
+ if (this._waitingForCallbacks.has(name)) {
92
+ this._waitingForCallbacks.get(name).push(args);
93
+ }
94
+ else {
95
+ this._waitingForCallbacks.set(name, [args]);
96
+ }
97
+ return false;
98
+ }
99
+ get $back() {
100
+ return undefined;
101
+ }
102
+ whenProviderDefined(name, consumer, callback) {
103
+ if (!name || typeof name !== 'string') {
104
+ throw new error_1.DeveloperException(`callback.invalid.name`, { type: typeof name });
105
+ }
106
+ if (!this._waitingForProviders.has(name)) {
107
+ this._waitingForProviders.set(name, []);
108
+ }
109
+ if (this._definedProviders.has(name)) {
110
+ const ins = this._definedProviders.get(name);
111
+ if (ins.producer !== consumer) {
112
+ callback(ins);
113
+ }
114
+ }
115
+ this._waitingForProviders.get(name).push({ consumer, callback });
116
+ }
117
+ defineProvider(name, producer, instance) {
118
+ if (!name || typeof name !== 'string') {
119
+ throw new error_1.DeveloperException(`callback.invalid.name`, { type: typeof name });
120
+ }
121
+ const ins = Object.assign(Object.assign({}, instance), { producer });
122
+ this._definedProviders.set(name, ins);
123
+ if (instance.proper) {
124
+ if (this._waitingForProviders.has(name)) {
125
+ this._waitingForProviders.get(name).forEach(item => {
126
+ if (item.consumer !== producer) {
127
+ item.callback(instance);
128
+ }
129
+ });
130
+ }
131
+ }
132
+ }
133
+ }
134
+ exports.CommonCallbackImpl = CommonCallbackImpl;
@@ -0,0 +1,75 @@
1
+ import { Arr, ShiftMain, ShiftSecure } from "../aliases";
2
+ import { Leyyo } from "../leyyo";
3
+ /**
4
+ * Callback repository, it collects all jobs and fire them when callback is defined
5
+ * */
6
+ export interface CommonCallback extends ShiftSecure<CommonCallbackSecure> {
7
+ /**
8
+ * Attach a callback
9
+ *
10
+ * @param {string} name
11
+ * @param {Function} fn
12
+ *
13
+ * Notes:
14
+ * - name should be regular string
15
+ * - After attached, it should call all waiting calls in the queue
16
+ * */
17
+ attachCallback(name: string, fn: Function): void;
18
+ /**
19
+ * Add a call into queue, which to be executed by appropriate callback
20
+ *
21
+ * @param {string} name
22
+ * @param {Array<any>} args
23
+ * @returns {boolean} - callback exists?
24
+ *
25
+ * Notes:
26
+ * - name should be regular string
27
+ * */
28
+ queueForCallback(name: string, ...args: Arr): boolean;
29
+ fqnName(value: any): string;
30
+ /**
31
+ * Informs when callback is changed
32
+ *
33
+ * @param {string} name
34
+ * @param {Function} consumer
35
+ * @param {CallbackWhenDefinedLambda} callback
36
+ *
37
+ * Notes:
38
+ * - name should be regular string
39
+ * - callback should be regular function
40
+ * - name should not be already defined
41
+ * */
42
+ whenProviderDefined<T extends CommonCallbackDefined = CommonCallbackDefined>(name: string, consumer: Function, callback: CallbackWhenDefinedLambda<T>): void;
43
+ /**
44
+ * Defines a provider to replace temporary providers
45
+ *
46
+ * @param {string} name
47
+ * @param {Function} producer
48
+ * @param {Object} instance
49
+ *
50
+ * Notes:
51
+ * - name should be regular string
52
+ * */
53
+ defineProvider<T extends CommonCallbackDefined = CommonCallbackDefined>(name: string, producer: Function, instance: T): void;
54
+ }
55
+ export interface CommonCallbackSecure extends ShiftMain<CommonCallback> {
56
+ $init(leyyo: Leyyo): void;
57
+ }
58
+ export interface CallbackRec {
59
+ initialization?: true;
60
+ fn: Function;
61
+ }
62
+ export interface CommonCallbackDefinedInternal extends CommonCallbackDefined {
63
+ producer: Function;
64
+ }
65
+ export interface CommonCallbackDefined {
66
+ proper: boolean;
67
+ }
68
+ export interface CommonCallbackDefinedItem {
69
+ consumer: Function;
70
+ callback: CallbackWhenDefinedLambda;
71
+ }
72
+ export interface CommonFqnCb extends CommonCallbackDefined {
73
+ name(value: any): string;
74
+ }
75
+ export type CallbackWhenDefinedLambda<T extends CommonCallbackDefined = CommonCallbackDefined> = (instance: T) => void;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from './index-types';
2
+ export * from './common-callback-impl';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./index-types"), exports);
18
+ __exportStar(require("./common-callback-impl"), exports);
@@ -0,0 +1,7 @@
1
+ export declare const LY_ATTACHED_ERROR = "#lyy.attached.error";
2
+ export declare const LY_PENDING_ERROR_REGISTER = "#lyy.pending.error.register";
3
+ export declare const LY_PENDING_ENUM_REGISTER = "#lyy.pending.enum.register";
4
+ export declare const LY_ATTACHED_FQN = "#lyy.attached.fqn";
5
+ export declare const LY_PENDING_FQN_REGISTER = "#lyy.pending.fqn.register";
6
+ export declare const LY_ATTACHED_LOG = "#lyy.attached.log";
7
+ export declare const LY_PENDING_LOG_REGISTER = "#lyy.pending.log.register";
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LY_PENDING_LOG_REGISTER = exports.LY_ATTACHED_LOG = exports.LY_PENDING_FQN_REGISTER = exports.LY_ATTACHED_FQN = exports.LY_PENDING_ENUM_REGISTER = exports.LY_PENDING_ERROR_REGISTER = exports.LY_ATTACHED_ERROR = void 0;
4
+ exports.LY_ATTACHED_ERROR = '#lyy.attached.error';
5
+ exports.LY_PENDING_ERROR_REGISTER = '#lyy.pending.error.register';
6
+ exports.LY_PENDING_ENUM_REGISTER = '#lyy.pending.enum.register';
7
+ exports.LY_ATTACHED_FQN = '#lyy.attached.fqn';
8
+ exports.LY_PENDING_FQN_REGISTER = '#lyy.pending.fqn.register';
9
+ exports.LY_ATTACHED_LOG = '#lyy.attached.log';
10
+ exports.LY_PENDING_LOG_REGISTER = '#lyy.pending.log.register';
@@ -0,0 +1,5 @@
1
+ import { Exception } from "./exception";
2
+ import { AssertionOpt } from "../assertion";
3
+ export declare class AssertionException extends Exception {
4
+ constructor(indicator?: string, params?: AssertionOpt);
5
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssertionException = void 0;
4
+ const exception_1 = require("./exception");
5
+ // noinspection Annotator
6
+ class AssertionException extends exception_1.Exception {
7
+ constructor(indicator, params) {
8
+ super(indicator, Object.assign(Object.assign({}, params), { indicator }));
9
+ }
10
+ }
11
+ exports.AssertionException = AssertionException;
@@ -0,0 +1,5 @@
1
+ import { Exception } from "./exception";
2
+ import { Dict } from "../aliases";
3
+ export declare class CausedException extends Exception {
4
+ constructor(e: Error, indicator?: string, params?: Dict);
5
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CausedException = void 0;
4
+ const exception_1 = require("./exception");
5
+ // noinspection Annotator
6
+ class CausedException extends exception_1.Exception {
7
+ constructor(e, indicator, params) {
8
+ super(e.message, Object.assign(Object.assign({}, params), { indicator }));
9
+ this.causedBy(e);
10
+ }
11
+ }
12
+ exports.CausedException = CausedException;
@@ -0,0 +1,24 @@
1
+ import { CommonError, CommonErrorSecure, ExceptionLike } from "./index-types";
2
+ import { Leyyo } from "../leyyo";
3
+ import { Dict } from "../aliases";
4
+ import { Exception } from "./exception";
5
+ export declare class CommonErrorImpl implements CommonError, CommonErrorSecure {
6
+ get $back(): CommonError;
7
+ $init(leyyo: Leyyo): void;
8
+ get $secure(): CommonErrorSecure;
9
+ addOmit(clz: Function, ...properties: Array<string>): boolean;
10
+ addSign(err: Error, ...keys: Array<string>): boolean;
11
+ afterCreate(e: ExceptionLike): void;
12
+ build(e: Error | string): ExceptionLike;
13
+ buildStack(e: Error): void;
14
+ copyStack(exception: Exception, error: Error): void;
15
+ causedBy(e: Error | string): ExceptionLike;
16
+ getOmit(clz: Function): Array<string>;
17
+ getSign(err: Error): Array<string>;
18
+ hasSign(err: Error, key: string): boolean;
19
+ inheritOmit(clz: Function): Array<string>;
20
+ initOmit(clz: Function): boolean;
21
+ initSign(err: Error): boolean;
22
+ removeSign(err: Error, ...keys: Array<string>): boolean;
23
+ toObject(e: Error, ...omittedFields: Array<string>): Dict;
24
+ }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommonErrorImpl = void 0;
4
+ const exception_1 = require("./exception");
5
+ const constants_1 = require("../constants");
6
+ // noinspection JSUnusedLocalSymbols
7
+ class CommonErrorImpl {
8
+ get $back() {
9
+ return this;
10
+ }
11
+ $init(leyyo) {
12
+ const fields = ['build', 'afterCreate', 'causedBy', 'toObject', 'buildStack', 'copyStack',
13
+ 'initSign', 'addSign', 'getSign', 'removeSign', 'hasSign',
14
+ 'initOmit', 'addOmit', 'getOmit', 'inheritOmit'];
15
+ const rec = { proper: false };
16
+ fields.forEach(field => {
17
+ rec[field] = this[field];
18
+ });
19
+ // define itself temporarily for error operations
20
+ leyyo.callback.defineProvider(constants_1.LY_ATTACHED_ERROR, CommonErrorImpl, rec);
21
+ // when new error provider is defined, replace all common methods
22
+ leyyo.callback.whenProviderDefined(constants_1.LY_ATTACHED_ERROR, CommonErrorImpl, (ins) => {
23
+ fields.forEach(field => {
24
+ if (typeof ins[field] === 'function') {
25
+ this[field] = ins[field];
26
+ }
27
+ });
28
+ });
29
+ }
30
+ get $secure() {
31
+ return this;
32
+ }
33
+ addOmit(clz, ...properties) {
34
+ return false;
35
+ }
36
+ addSign(err, ...keys) {
37
+ return false;
38
+ }
39
+ afterCreate(e) {
40
+ e.$secure.$setName(e.constructor.name);
41
+ }
42
+ build(e) {
43
+ if (e instanceof exception_1.Exception) {
44
+ return e;
45
+ }
46
+ else if (e instanceof Error) {
47
+ const err = new exception_1.Exception(e.message, this.toObject(e, 'message', 'stack'));
48
+ this.copyStack(err, e);
49
+ return err;
50
+ }
51
+ else if (typeof e === 'string') {
52
+ return new exception_1.Exception(e);
53
+ }
54
+ return new exception_1.Exception(`Unknown error`, Object.assign(Object.assign({}, this.toObject(e)), { type: typeof e }));
55
+ }
56
+ buildStack(e) {
57
+ }
58
+ copyStack(exception, error) {
59
+ }
60
+ causedBy(e) {
61
+ return undefined;
62
+ }
63
+ getOmit(clz) {
64
+ return undefined;
65
+ }
66
+ getSign(err) {
67
+ return undefined;
68
+ }
69
+ hasSign(err, key) {
70
+ return false;
71
+ }
72
+ inheritOmit(clz) {
73
+ return undefined;
74
+ }
75
+ initOmit(clz) {
76
+ return false;
77
+ }
78
+ initSign(err) {
79
+ return false;
80
+ }
81
+ removeSign(err, ...keys) {
82
+ return false;
83
+ }
84
+ toObject(e, ...omittedFields) {
85
+ return undefined;
86
+ }
87
+ }
88
+ exports.CommonErrorImpl = CommonErrorImpl;
@@ -0,0 +1,5 @@
1
+ import { Exception } from "./exception";
2
+ import { AssertionOpt } from "../assertion";
3
+ export declare class DeveloperException extends Exception {
4
+ constructor(indicator?: string, params?: AssertionOpt);
5
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeveloperException = void 0;
4
+ const exception_1 = require("./exception");
5
+ // noinspection Annotator
6
+ class DeveloperException extends exception_1.Exception {
7
+ constructor(indicator, params) {
8
+ super(indicator, Object.assign(Object.assign({}, params), { indicator }));
9
+ }
10
+ }
11
+ exports.DeveloperException = DeveloperException;
@@ -0,0 +1,33 @@
1
+ import { CommonError, ExceptionLike, ExceptionParamsAppend, ExceptionSecure, ExceptionStackLine } from "./index-types";
2
+ import { Abstract, ClassLike, ClassOrName, Dict } from "../aliases";
3
+ import { Leyyo } from "../leyyo";
4
+ export declare class Exception extends Error implements ExceptionLike, ExceptionSecure {
5
+ private static _callback;
6
+ private static _error;
7
+ private static _log;
8
+ private static _assertion;
9
+ protected _params: Dict;
10
+ protected _parsed: Array<ExceptionStackLine>;
11
+ protected _holder?: ClassOrName;
12
+ protected _cause?: ExceptionLike;
13
+ protected _req?: unknown;
14
+ constructor(message: string, params?: Dict);
15
+ get params(): Dict;
16
+ $setName(name: string): this;
17
+ causedBy(e: Error | string): this;
18
+ with(value: ClassLike | Abstract<any> | string | any): this;
19
+ appendParams(params: ExceptionParamsAppend, ignoreExisting?: boolean): this;
20
+ log(req?: unknown): this;
21
+ raise(throwable?: boolean, req?: unknown): this;
22
+ toObject(...omittedFields: Array<string>): Dict;
23
+ toJSON(): Dict;
24
+ static cast(e: string | Error): ExceptionLike;
25
+ $hasSign(key: string): boolean;
26
+ $listSigns(): Array<string>;
27
+ $addSign(...keys: Array<string>): boolean;
28
+ $removeSign(...keys: Array<string>): boolean;
29
+ static $setLeyyo(leyyo: Leyyo): void;
30
+ static get $error(): CommonError;
31
+ get $back(): ExceptionLike;
32
+ get $secure(): ExceptionSecure;
33
+ }
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Exception = void 0;
4
+ class Exception extends Error {
5
+ constructor(message, params) {
6
+ if (global === null || global === void 0 ? void 0 : global.leyyo_is_testing) {
7
+ message += ` => ${JSON.stringify(Exception._assertion.secureJson(params))}`;
8
+ }
9
+ super(message);
10
+ this.name = Exception._callback.fqnName(this);
11
+ this._params = params !== null && params !== void 0 ? params : {};
12
+ this._parsed = [];
13
+ Exception._error.afterCreate(this);
14
+ }
15
+ get params() {
16
+ return this._params;
17
+ }
18
+ $setName(name) {
19
+ if (typeof name === 'string') {
20
+ this.name = name;
21
+ }
22
+ return this;
23
+ }
24
+ // noinspection JSUnusedLocalSymbols
25
+ causedBy(e) {
26
+ this._cause = Exception._error.causedBy(e);
27
+ return this;
28
+ }
29
+ with(value) {
30
+ this._holder = Exception._callback.fqnName(value);
31
+ return this;
32
+ }
33
+ appendParams(params, ignoreExisting) {
34
+ var _a;
35
+ this._params = (_a = this._params) !== null && _a !== void 0 ? _a : {};
36
+ try {
37
+ for (const [k, v] of Object.entries(params)) {
38
+ if (!(ignoreExisting && this._params[k] !== undefined)) {
39
+ this._params[k] = v;
40
+ }
41
+ }
42
+ }
43
+ catch (e) {
44
+ }
45
+ return this;
46
+ }
47
+ log(req) {
48
+ if (this.$hasSign('printed')) {
49
+ return this;
50
+ }
51
+ if (req) {
52
+ this._req = req;
53
+ }
54
+ const params = {};
55
+ if (this._req) {
56
+ params['req'] = this._req;
57
+ }
58
+ // todo collect properties
59
+ const line = { severity: 'error', message: this, params, holder: this._holder };
60
+ this.$addSign('printed');
61
+ Exception._log.apply(line);
62
+ return this;
63
+ }
64
+ raise(throwable = true, req) {
65
+ if (!throwable) {
66
+ if (req) {
67
+ this._req = req;
68
+ }
69
+ this.log();
70
+ return undefined;
71
+ }
72
+ throw this;
73
+ }
74
+ toObject(...omittedFields) {
75
+ return Exception._error.toObject(this, ...omittedFields);
76
+ }
77
+ toJSON() {
78
+ try {
79
+ return this.toObject();
80
+ }
81
+ catch (e) {
82
+ return { name: this.name, message: this.message, params: this._params };
83
+ }
84
+ }
85
+ static cast(e) {
86
+ return Exception._error.build(e);
87
+ }
88
+ $hasSign(key) {
89
+ return Exception._error.hasSign(this, key);
90
+ }
91
+ $listSigns() {
92
+ return Exception._error.getSign(this);
93
+ }
94
+ $addSign(...keys) {
95
+ return Exception._error.addSign(this, ...keys);
96
+ }
97
+ $removeSign(...keys) {
98
+ return Exception._error.removeSign(this, ...keys);
99
+ }
100
+ static $setLeyyo(leyyo) {
101
+ if (!this._callback) {
102
+ this._callback = leyyo.callback;
103
+ }
104
+ if (!this._error) {
105
+ this._error = leyyo.error;
106
+ }
107
+ if (!this._log) {
108
+ this._log = leyyo.log;
109
+ }
110
+ if (!this._assertion) {
111
+ this._assertion = leyyo.assertion;
112
+ }
113
+ }
114
+ static get $error() {
115
+ return this._error;
116
+ }
117
+ get $back() {
118
+ return this;
119
+ }
120
+ get $secure() {
121
+ return this;
122
+ }
123
+ }
124
+ exports.Exception = Exception;
@@ -0,0 +1,70 @@
1
+ import { Leyyo } from "../leyyo";
2
+ import { Abstract, ClassLike, Dict, ShiftMain, ShiftSecure } from "../aliases";
3
+ import { Exception } from "./exception";
4
+ import { CommonCallbackDefined } from "../callback";
5
+ export interface CommonError extends ShiftSecure<CommonErrorSecure> {
6
+ build(e: Error | string): ExceptionLike;
7
+ afterCreate(e: ExceptionLike): void;
8
+ causedBy(e: Error | string): ExceptionLike;
9
+ initSign(err: Error): boolean;
10
+ addSign(err: Error, ...keys: Array<string>): boolean;
11
+ getSign(err: Error): Array<string>;
12
+ removeSign(err: Error, ...keys: Array<string>): boolean;
13
+ hasSign(err: Error, key: string): boolean;
14
+ toObject(e: Error, ...omittedFields: Array<string>): Dict;
15
+ buildStack(e: Error): void;
16
+ copyStack(exception: Exception, error: Error): void;
17
+ initOmit(clz: Function): boolean;
18
+ addOmit(clz: Function, ...properties: Array<string>): boolean;
19
+ getOmit(clz: Function): Array<string>;
20
+ inheritOmit(clz: Function): Array<string>;
21
+ }
22
+ export interface CommonErrorCb extends CommonCallbackDefined {
23
+ build?(e: Error | string): ExceptionLike;
24
+ afterCreate?(e: ExceptionLike): void;
25
+ causedBy?(e: Error | string): ExceptionLike;
26
+ initSign?(err: Error): boolean;
27
+ addSign?(err: Error, ...keys: Array<string>): boolean;
28
+ getSign?(err: Error): Array<string>;
29
+ removeSign?(err: Error, ...keys: Array<string>): boolean;
30
+ hasSign?(err: Error, key: string): boolean;
31
+ toObject?(e: Error, ...omittedFields: Array<string>): Dict;
32
+ buildStack?(e: Error): void;
33
+ copyStack?(exception: Exception, error: Error): void;
34
+ initOmit?(clz: Function): boolean;
35
+ addOmit?(clz: Function, ...properties: Array<string>): boolean;
36
+ getOmit?(clz: Function): Array<string>;
37
+ inheritOmit?(clz: Function): Array<string>;
38
+ }
39
+ export interface CommonErrorSecure extends ShiftMain<CommonError> {
40
+ $init(leyyo: Leyyo): void;
41
+ }
42
+ export interface ExceptionStackLine {
43
+ file?: string;
44
+ method?: string;
45
+ line?: number;
46
+ column?: number;
47
+ }
48
+ export interface ExceptionParamsAppend extends Dict {
49
+ indicator?: string;
50
+ }
51
+ export interface ExceptionLike extends Error, ShiftSecure<ExceptionSecure> {
52
+ get params(): Dict;
53
+ causedBy(e: Error | string): this;
54
+ with(clazz: ClassLike): this;
55
+ with(instance: Abstract<any>): this;
56
+ with(name: string): this;
57
+ with(value: any): this;
58
+ appendParams(params: ExceptionParamsAppend, ignoreExisting?: boolean): this;
59
+ log(req?: unknown): this;
60
+ raise(throwable?: boolean, req?: unknown): this;
61
+ toObject(...omittedFields: Array<string>): Dict;
62
+ toJSON(): Dict;
63
+ }
64
+ export interface ExceptionSecure extends ShiftMain<ExceptionLike> {
65
+ $setName(name: string): this;
66
+ $hasSign(key: string): boolean;
67
+ $listSigns(): Array<string>;
68
+ $addSign(...keys: Array<string>): boolean;
69
+ $removeSign(...keys: Array<string>): boolean;
70
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ export * from './index-types';
2
+ export * from './common-error-impl';
3
+ export * from './exception';
4
+ export * from './caused-exception';
5
+ export * from './developer-exception';
6
+ export * from './assertion-exception';
7
+ export * from './multiple-exception';