@cofondateurauchomage/libs 1.1.119 → 1.1.121

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.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Helper pour récupérer le code number d'une erreur
3
+ */
4
+ export declare function errCode(e: unknown): number;
5
+ /**
6
+ * Helper pour récupérer le code string d'une erreur
7
+ */
8
+ export declare function errStringCode(e: unknown): string | undefined;
9
+ /**
10
+ * Helper pour récupérer le message d'une erreur
11
+ */
12
+ export declare function errMessage(e: unknown): string;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errCode = errCode;
4
+ exports.errStringCode = errStringCode;
5
+ exports.errMessage = errMessage;
6
+ const arrayUtils_1 = require("./arrayUtils");
7
+ const objectUtils_1 = require("./objectUtils");
8
+ const dataInKeys = ["response", "body", "data", "error"];
9
+ const codeKeys = ["code", "status", "statusCode", "status_code"];
10
+ const messageKeys = ["message", "detail", "description", "error_description", "errorMessage"];
11
+ function asRecord(value) {
12
+ return (0, objectUtils_1.isObject)(value) ? value : undefined;
13
+ }
14
+ /**
15
+ * Construit les objets candidats à inspecter pour retrouver code/message :
16
+ * - error
17
+ * - error[dataInKeys]
18
+ * - error[codeKeys]
19
+ * - error[dataInKeys][dataInKeys]
20
+ * - error[dataInKeys][codeKeys]
21
+ */
22
+ function getErrorCandidates(e) {
23
+ const root = asRecord(e);
24
+ if (!root) {
25
+ return [];
26
+ }
27
+ const directData = (0, arrayUtils_1.compact)(dataInKeys.map(key => asRecord(root[key])));
28
+ const directCode = (0, arrayUtils_1.compact)(codeKeys.map(key => asRecord(root[key])));
29
+ const nestedData = directData.flatMap(data => (0, arrayUtils_1.compact)(dataInKeys.map(key => asRecord(data[key]))));
30
+ const nestedCode = directData.flatMap(data => (0, arrayUtils_1.compact)(codeKeys.map(key => asRecord(data[key]))));
31
+ return [root, ...directData, ...directCode, ...nestedData, ...nestedCode];
32
+ }
33
+ /**
34
+ * Helper pour récupérer le code number d'une erreur
35
+ */
36
+ function errCode(e) {
37
+ const candidates = getErrorCandidates(e);
38
+ const code = candidates?.flatMap(candidate => [candidate, ...codeKeys.map(key => candidate[key])]).find(value => typeof value === "number");
39
+ return code ?? 500;
40
+ }
41
+ /**
42
+ * Helper pour récupérer le code string d'une erreur
43
+ */
44
+ function errStringCode(e) {
45
+ const candidates = getErrorCandidates(e);
46
+ const code = candidates?.flatMap(candidate => [candidate, ...codeKeys.map(key => candidate[key])]).find(value => typeof value === "string");
47
+ return code;
48
+ }
49
+ /**
50
+ * Helper pour récupérer le message d'une erreur
51
+ */
52
+ function errMessage(e) {
53
+ const candidates = getErrorCandidates(e);
54
+ const stringErr = candidates?.flatMap(candidate => [candidate, ...messageKeys.map(key => candidate[key])]).find(value => typeof value === "string");
55
+ return stringErr ?? JSON.stringify(e).trim().slice(0, 100);
56
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./arrayUtils";
2
+ export * from "./errorUtils";
2
3
  export * from "./objectUtils";
3
4
  export * from "./stringUtils";
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./arrayUtils"), exports);
18
+ __exportStar(require("./errorUtils"), exports);
18
19
  __exportStar(require("./objectUtils"), exports);
19
20
  __exportStar(require("./stringUtils"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cofondateurauchomage/libs",
3
- "version": "1.1.119",
3
+ "version": "1.1.121",
4
4
  "description": "",
5
5
  "main": "build/index",
6
6
  "scripts": {