@rotomeca/rop 5.0.2 → 6.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.
@@ -41,5 +41,14 @@ class ATresult {
41
41
  static Fail(error) {
42
42
  return internal_1.Fail.Create(error ?? new Error("An error occurred"));
43
43
  }
44
+ /**
45
+ * Throw an error from an Error object or a text.
46
+ * @param error Error to throw
47
+ * @throws An Error
48
+ */
49
+ static Throw(error = undefined) {
50
+ const err = typeof error === 'string' ? new Error(error) : error;
51
+ this.Fail(err).throwIfError();
52
+ }
44
53
  }
45
54
  exports.ATresult = ATresult;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ // type: decorators
3
+ // description: Decorators to log exceptions with a specific format before re-throwing them.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.LogOnError = LogOnError;
6
+ exports.LogOnErrorAsync = LogOnErrorAsync;
7
+ /**
8
+ * Decorator to log errors occurring during the execution of a synchronous method.
9
+ * Logs the error in the format: ### [methodName]what
10
+ * * @param logger - The logger function to use (default: console.error).
11
+ * @returns A method decorator that logs errors and re-throws them.
12
+ */
13
+ function LogOnError(logger = console.error) {
14
+ return function (originalMethod, context) {
15
+ if (context.kind !== "method") {
16
+ throw new Error("LogOnError can only be applied to methods.");
17
+ }
18
+ const methodName = String(context.name);
19
+ return function (...args) {
20
+ try {
21
+ return originalMethod.call(this, ...args);
22
+ }
23
+ catch (error) {
24
+ logger(`### [${methodName}]${error}`);
25
+ throw error;
26
+ }
27
+ };
28
+ };
29
+ }
30
+ /**
31
+ * Decorator to log errors occurring during the execution of an asynchronous method.
32
+ * Logs the error in the format: ### [methodName]what
33
+ * * @param logger - The logger function to use (default: console.error).
34
+ * @returns A method decorator that logs errors and re-throws them.
35
+ */
36
+ function LogOnErrorAsync(logger = console.error) {
37
+ return function (originalMethod, context) {
38
+ if (context.kind !== "method") {
39
+ throw new Error("LogOnErrorAsync can only be applied to methods.");
40
+ }
41
+ const methodName = String(context.name);
42
+ return async function (...args) {
43
+ try {
44
+ return await originalMethod.call(this, ...args);
45
+ }
46
+ catch (error) {
47
+ logger(`### [${methodName}]${error}`);
48
+ throw error;
49
+ }
50
+ };
51
+ };
52
+ }
@@ -87,4 +87,21 @@ export declare abstract class ATresult<Success, E = Error> implements ITResult<S
87
87
  * @returns A new Fail instance containing the error.
88
88
  */
89
89
  static Fail<TSuccess, TE = Error>(error?: TE): ITResult<TSuccess, TE>;
90
+ /**
91
+ * Throw an error from a text.
92
+ * @param msg Error text
93
+ * @throws The created error
94
+ */
95
+ static Throw(msg: string): void;
96
+ /**
97
+ * Throw an error
98
+ * @param error Error to throw
99
+ * @throws The error
100
+ */
101
+ static Throw<TE extends Error>(error: TE): void;
102
+ /**
103
+ * Throw an error
104
+ * @throws An error
105
+ */
106
+ static Throw(): void;
90
107
  }
@@ -38,4 +38,13 @@ export class ATresult {
38
38
  static Fail(error) {
39
39
  return Fail.Create(error ?? new Error("An error occurred"));
40
40
  }
41
+ /**
42
+ * Throw an error from an Error object or a text.
43
+ * @param error Error to throw
44
+ * @throws An Error
45
+ */
46
+ static Throw(error = undefined) {
47
+ const err = typeof error === 'string' ? new Error(error) : error;
48
+ this.Fail(err).throwIfError();
49
+ }
41
50
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Type définition for a logger function.
3
+ */
4
+ type LoggerFunction = (message: string) => void;
5
+ /**
6
+ * Decorator to log errors occurring during the execution of a synchronous method.
7
+ * Logs the error in the format: ### [methodName]what
8
+ * * @param logger - The logger function to use (default: console.error).
9
+ * @returns A method decorator that logs errors and re-throws them.
10
+ */
11
+ export declare function LogOnError(logger?: LoggerFunction): (originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => any;
12
+ /**
13
+ * Decorator to log errors occurring during the execution of an asynchronous method.
14
+ * Logs the error in the format: ### [methodName]what
15
+ * * @param logger - The logger function to use (default: console.error).
16
+ * @returns A method decorator that logs errors and re-throws them.
17
+ */
18
+ export declare function LogOnErrorAsync(logger?: LoggerFunction): (originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => Promise<any>;
19
+ export {};
@@ -0,0 +1,48 @@
1
+ // type: decorators
2
+ // description: Decorators to log exceptions with a specific format before re-throwing them.
3
+ /**
4
+ * Decorator to log errors occurring during the execution of a synchronous method.
5
+ * Logs the error in the format: ### [methodName]what
6
+ * * @param logger - The logger function to use (default: console.error).
7
+ * @returns A method decorator that logs errors and re-throws them.
8
+ */
9
+ export function LogOnError(logger = console.error) {
10
+ return function (originalMethod, context) {
11
+ if (context.kind !== "method") {
12
+ throw new Error("LogOnError can only be applied to methods.");
13
+ }
14
+ const methodName = String(context.name);
15
+ return function (...args) {
16
+ try {
17
+ return originalMethod.call(this, ...args);
18
+ }
19
+ catch (error) {
20
+ logger(`### [${methodName}]${error}`);
21
+ throw error;
22
+ }
23
+ };
24
+ };
25
+ }
26
+ /**
27
+ * Decorator to log errors occurring during the execution of an asynchronous method.
28
+ * Logs the error in the format: ### [methodName]what
29
+ * * @param logger - The logger function to use (default: console.error).
30
+ * @returns A method decorator that logs errors and re-throws them.
31
+ */
32
+ export function LogOnErrorAsync(logger = console.error) {
33
+ return function (originalMethod, context) {
34
+ if (context.kind !== "method") {
35
+ throw new Error("LogOnErrorAsync can only be applied to methods.");
36
+ }
37
+ const methodName = String(context.name);
38
+ return async function (...args) {
39
+ try {
40
+ return await originalMethod.call(this, ...args);
41
+ }
42
+ catch (error) {
43
+ logger(`### [${methodName}]${error}`);
44
+ throw error;
45
+ }
46
+ };
47
+ };
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rotomeca/rop",
3
- "version": "5.0.2",
3
+ "version": "6.0.0",
4
4
  "type": "module",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",