topkat-utils 1.2.16 → 1.2.18

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### v1.2.17
2
+ * added a relevant toString method to DescriptiveError class
3
+ * improved error logging
4
+
1
5
  ### v1.2.16
2
6
  * ADD onError to config for custom error handling
3
7
  * improve logging
@@ -15,7 +15,9 @@ export declare class DescriptiveError extends Error {
15
15
  msg: string;
16
16
  options: ErrorOptions;
17
17
  hasBeenLogged: boolean;
18
+ logs: string[];
18
19
  constructor(msg: string, options?: ErrorOptions);
19
- log(doNotCountHasLogged?: boolean): void;
20
- toString(): void;
20
+ parseError(forCli?: boolean): void;
21
+ log(): void;
22
+ toString(): string;
21
23
  }
@@ -57,12 +57,16 @@ async function tryCatch(callback, onErr = () => { }) {
57
57
  }
58
58
  exports.tryCatch = tryCatch;
59
59
  function extraInfosRendererDefault(extraInfos) {
60
- logger_utils_1.C.error(false, '== EXTRA INFOS ==');
61
- logger_utils_1.C.error(false, JSON.stringify(extraInfos, null, 2));
60
+ return [
61
+ '== EXTRA INFOS ==',
62
+ JSON.stringify(extraInfos, null, 2)
63
+ ];
62
64
  }
63
65
  class DescriptiveError extends Error {
64
66
  constructor(msg, options = {}) {
65
67
  super(msg);
68
+ this.hasBeenLogged = false;
69
+ this.logs = [];
66
70
  delete options.errMsgId;
67
71
  this.msg = msg;
68
72
  const { doNotWaitOneFrameForLog = false } = options, optionsClean = __rest(options, ["doNotWaitOneFrameForLog"]);
@@ -79,59 +83,66 @@ class DescriptiveError extends Error {
79
83
  });
80
84
  const { onError = () => { } } = (0, config_1.configFn)();
81
85
  onError(msg, options);
86
+ this.parseError();
82
87
  }
83
- log(doNotCountHasLogged = false) {
84
- if (!this.hasBeenLogged) {
85
- const _a = this.options, { err, doNotThrow = false, noStackTrace = false, ressource, extraInfosRenderer = extraInfosRendererDefault, notifyOnSlackChannel = false, originalMessage } = _a, extraInfosRaw = __rest(_a, ["err", "doNotThrow", "noStackTrace", "ressource", "extraInfosRenderer", "notifyOnSlackChannel", "originalMessage"]);
86
- let { code } = this.options;
87
- const extraInfos = Object.assign(Object.assign({}, extraInfosRaw), (this.options.extraInfos || {}));
88
- if (!(0, isset_1.isset)(extraInfos.value) && this.options.hasOwnProperty('value'))
89
- extraInfos.value = 'undefined';
90
- if (!(0, isset_1.isset)(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue'))
91
- extraInfos.gotValue = 'undefined';
92
- if ((0, isset_1.isset)(ressource)) {
93
- code = 404;
94
- if (this.msg === '404')
95
- this.msg = `Ressource ${ressource} not found`;
96
- extraInfos.ressource = ressource;
97
- }
98
- logger_utils_1.C.error(false, this.msg || this.message);
99
- if (Object.keys(extraInfos).length > 0)
100
- extraInfosRenderer(extraInfos);
101
- if (err) {
102
- logger_utils_1.C.error(false, '== ORIGINAL ERROR ==');
103
- if (err.log) {
104
- err.hasBeenLogged = false;
105
- err.log();
106
- }
107
- else {
108
- logErr(noStackTrace, err);
109
- if (err.extraInfos)
110
- logErr(noStackTrace, err.extraInfos);
111
- }
88
+ parseError(forCli = false) {
89
+ const errorLogs = [];
90
+ const _a = this.options, { err, doNotThrow = false, noStackTrace = false, ressource, extraInfosRenderer = extraInfosRendererDefault, notifyOnSlackChannel = false, originalMessage } = _a, extraInfosRaw = __rest(_a, ["err", "doNotThrow", "noStackTrace", "ressource", "extraInfosRenderer", "notifyOnSlackChannel", "originalMessage"]);
91
+ let { code } = this.options;
92
+ const extraInfos = Object.assign(Object.assign({}, extraInfosRaw), (this.options.extraInfos || {}));
93
+ this.code = code || 500;
94
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !(0, isset_1.isset)(this.options.code)))
95
+ delete this.code;
96
+ if (!(0, isset_1.isset)(extraInfos.value) && this.options.hasOwnProperty('value'))
97
+ extraInfos.value = 'undefined';
98
+ if (!(0, isset_1.isset)(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue'))
99
+ extraInfos.gotValue = 'undefined';
100
+ if ((0, isset_1.isset)(ressource)) {
101
+ code = 404;
102
+ if (this.msg === '404')
103
+ this.msg = `Ressource ${ressource} not found`;
104
+ extraInfos.ressource = ressource;
105
+ }
106
+ errorLogs.push(this.msg || this.message);
107
+ if (Object.keys(extraInfos).length > 0)
108
+ extraInfosRenderer(extraInfos);
109
+ if (err) {
110
+ errorLogs.push('== ORIGINAL ERROR ==');
111
+ if (typeof err.parseError === 'function') {
112
+ err.hasBeenLogged = false;
113
+ const logFromOtherErr = err.parseError(forCli);
114
+ errorLogs.push(...logFromOtherErr);
112
115
  }
113
116
  else {
114
- if (!noStackTrace)
115
- logger_utils_1.C.error(false, logger_utils_1.C.dim((0, clean_stack_trace_1.cleanStackTrace)(extraInfosRaw.stack || this.stack)));
117
+ errorLogs.push(err.toString());
118
+ if (!noStackTrace && err.stack)
119
+ errorLogs.push(err.stack);
120
+ if (err.extraInfos)
121
+ errorLogs.push(err.extraInfos);
116
122
  }
117
- this.code = code || 500;
118
- if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !(0, isset_1.isset)(this.options.code)))
119
- delete this.code;
120
- this.errorDescription = Object.assign({ msg: this.msg, code,
121
- ressource }, extraInfos);
122
123
  }
123
- if (!doNotCountHasLogged)
124
- this.hasBeenLogged = true;
124
+ else {
125
+ if (!noStackTrace) {
126
+ const stackTranceClean = (0, clean_stack_trace_1.cleanStackTrace)(extraInfosRaw.stack || this.stack);
127
+ errorLogs.push(forCli ? logger_utils_1.C.dim(stackTranceClean) : stackTranceClean);
128
+ }
129
+ }
130
+ // THIS is used to access error as object
131
+ this.code = code || 500;
132
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !(0, isset_1.isset)(this.options.code)))
133
+ delete this.code;
134
+ this.errorDescription = Object.assign({ msg: this.msg, code,
135
+ ressource }, extraInfos);
136
+ this.logs = errorLogs;
137
+ }
138
+ log() {
139
+ if (!this.hasBeenLogged)
140
+ this.logs.forEach(errLine => logger_utils_1.C.error(false, errLine));
141
+ this.hasBeenLogged = true;
125
142
  }
126
143
  toString() {
127
- return this.log(true);
144
+ return this.logs.join('\n');
128
145
  }
129
146
  }
130
147
  exports.DescriptiveError = DescriptiveError;
131
- function logErr(noStackTrace, ...params) {
132
- if (noStackTrace)
133
- logger_utils_1.C.error(false, ...params);
134
- else
135
- logger_utils_1.C.error(...params);
136
- }
137
148
  //# sourceMappingURL=error-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-utils.js","sourceRoot":"","sources":["../../src/error-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,0CAA0C;AAC1C,cAAc;AACd,0CAA0C;AAC1C,qCAAmC;AACnC,mCAA+B;AAC/B,yCAAoC;AAEpC,2DAAqD;AACrD,iDAAkC;AAGlC,SAAgB,WAAW,CAAC,uBAAuB,IAAI,OAAO,cAAc,CAAC,GAAG,EAAE,KAAK,EAAE,uBAAuB,CAAC,CAAA,CAAC,CAAC;AAAnH,kCAAmH;AAEnH,SAAgB,cAAc,CAAC,uBAAuB,IAAI,OAAO,cAAc,CAAC,GAAG,EAAE,KAAK,EAAE,uBAAuB,CAAC,CAAA,CAAC,CAAC;AAAtH,wCAAsH;AAEtH,SAAgB,kBAAkB,CAAC,uBAAuB,IAAI,OAAO,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,uBAAuB,CAAC,CAAA,CAAC,CAAC;AAAzH,gDAAyH;AAEzH,SAAgB,qBAAqB,CAAC,uBAAuB,IAAI,OAAO,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,uBAAuB,CAAC,CAAA,CAAC,CAAC;AAA5H,sDAA4H;AAE5H,SAAgB,cAAc,CAAC,OAAO,EAAE,UAAU,EAAE,uBAAuB;IACvE,IAAI,WAAW,GAAG,EAAE,CAAA;IACpB,KAAK,IAAI,IAAI,IAAI,uBAAuB,EAAE;QACtC,IAAI,CAAC,IAAA,aAAK,EAAC,uBAAuB,CAAC,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,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,CAAA;gBAC3E,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,gBAAC,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAA;aACtE;SACJ;QAED,yCAAyC;QACzC,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,GAAG,CAAA;QACvB,IAAI,IAAI,CAAC,OAAO,CAAC,gBAAgB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QACzH,IAAI,CAAC,gBAAgB,mBACjB,GAAG,EAAE,IAAI,CAAC,GAAG,EACb,IAAI;YACJ,SAAS,IACN,UAAU,CAChB,CAAA;QAED,IAAI,CAAC,IAAI,GAAG,SAAS,CAAA;IACzB,CAAC;IACD,GAAG;QACC,IAAI,CAAC,IAAI,CAAC,aAAa;YAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,gBAAC,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAA;QAC9E,IAAI,CAAC,aAAa,GAAG,IAAI,CAAA;IAC7B,CAAC;IACD,QAAQ;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;CACJ;AArFD,4CAqFC"}
@@ -9,7 +9,7 @@ export declare type ErrorOptions = {
9
9
  code?: number;
10
10
  doNotDisplayCode?: boolean;
11
11
  notifyOnSlackChannel?: boolean;
12
- extraInfosRenderer?: (extraInfosObj: ObjectGeneric) => void;
12
+ extraInfosRenderer?: (extraInfosObj: ObjectGeneric) => string[];
13
13
  doNotWaitOneFrameForLog?: boolean;
14
14
  noStackTrace?: boolean;
15
15
  [k: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topkat-utils",
3
- "version": "1.2.16",
3
+ "version": "1.2.18",
4
4
  "type": "commonjs",
5
5
  "types": "index.ts",
6
6
  "main": "dist",
@@ -41,11 +41,11 @@ export async function tryCatch(callback: Function, onErr: Function = () => { })
41
41
  }
42
42
 
43
43
 
44
-
45
-
46
44
  function extraInfosRendererDefault(extraInfos) {
47
- C.error(false, '== EXTRA INFOS ==')
48
- C.error(false, JSON.stringify(extraInfos, null, 2))
45
+ return [
46
+ '== EXTRA INFOS ==',
47
+ JSON.stringify(extraInfos, null, 2)
48
+ ]
49
49
  }
50
50
 
51
51
  export class DescriptiveError extends Error {
@@ -53,7 +53,8 @@ export class DescriptiveError extends Error {
53
53
  code: number
54
54
  msg: string
55
55
  options: ErrorOptions
56
- hasBeenLogged: boolean
56
+ hasBeenLogged: boolean = false
57
+ logs: string[] = []
57
58
 
58
59
  constructor(msg: string, options: ErrorOptions = {}) {
59
60
  super(msg)
@@ -70,53 +71,66 @@ export class DescriptiveError extends Error {
70
71
 
71
72
  const { onError = () => { } } = configFn()
72
73
  onError(msg, options)
74
+
75
+ this.parseError()
73
76
  }
74
- log(doNotCountHasLogged = false) {
75
- if (!this.hasBeenLogged) {
76
- const { err, doNotThrow = false, noStackTrace = false, ressource, extraInfosRenderer = extraInfosRendererDefault, notifyOnSlackChannel = false, originalMessage, ...extraInfosRaw } = this.options
77
- let { code } = this.options
78
- const extraInfos = { ...extraInfosRaw, ...(this.options.extraInfos || {}) }
79
-
80
- if (!isset(extraInfos.value) && this.options.hasOwnProperty('value')) extraInfos.value = 'undefined'
81
- if (!isset(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue')) extraInfos.gotValue = 'undefined'
82
-
83
- if (isset(ressource)) {
84
- code = 404
85
- if (this.msg === '404') this.msg = `Ressource ${ressource} not found`
86
- extraInfos.ressource = ressource
87
- }
77
+ parseError(forCli = false) {
78
+
79
+ const errorLogs: string[] = []
80
+
81
+ const { err, doNotThrow = false, noStackTrace = false, ressource, extraInfosRenderer = extraInfosRendererDefault, notifyOnSlackChannel = false, originalMessage, ...extraInfosRaw } = this.options
82
+ let { code } = this.options
83
+ const extraInfos = { ...extraInfosRaw, ...(this.options.extraInfos || {}) }
84
+
85
+ this.code = code || 500
86
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !isset(this.options.code))) delete this.code
87
+
88
+ if (!isset(extraInfos.value) && this.options.hasOwnProperty('value')) extraInfos.value = 'undefined'
89
+ if (!isset(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue')) extraInfos.gotValue = 'undefined'
88
90
 
89
- C.error(false, this.msg || this.message)
90
- if (Object.keys(extraInfos).length > 0) extraInfosRenderer(extraInfos)
91
- if (err) {
92
- C.error(false, '== ORIGINAL ERROR ==')
93
- if (err.log) {
94
- err.hasBeenLogged = false
95
- err.log()
96
- } else {
97
- logErr(noStackTrace, err)
98
- if (err.extraInfos) logErr(noStackTrace, err.extraInfos)
99
- }
91
+ if (isset(ressource)) {
92
+ code = 404
93
+ if (this.msg === '404') this.msg = `Ressource ${ressource} not found`
94
+ extraInfos.ressource = ressource
95
+ }
96
+
97
+ errorLogs.push(this.msg || this.message)
98
+ if (Object.keys(extraInfos).length > 0) extraInfosRenderer(extraInfos)
99
+ if (err) {
100
+ errorLogs.push('== ORIGINAL ERROR ==')
101
+ if (typeof err.parseError === 'function') {
102
+ err.hasBeenLogged = false
103
+ const logFromOtherErr = err.parseError(forCli)
104
+ errorLogs.push(...logFromOtherErr)
100
105
  } else {
101
- if (!noStackTrace) C.error(false, C.dim(cleanStackTrace(extraInfosRaw.stack || this.stack)))
106
+ errorLogs.push(err.toString())
107
+ if (!noStackTrace && err.stack) errorLogs.push(err.stack)
108
+ if (err.extraInfos) errorLogs.push(err.extraInfos)
102
109
  }
103
- this.code = code || 500
104
- if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !isset(this.options.code))) delete this.code
105
- this.errorDescription = {
106
- msg: this.msg,
107
- code,
108
- ressource,
109
- ...extraInfos,
110
+ } else {
111
+ if (!noStackTrace) {
112
+ const stackTranceClean = cleanStackTrace(extraInfosRaw.stack || this.stack)
113
+ errorLogs.push(forCli ? C.dim(stackTranceClean) : stackTranceClean)
110
114
  }
111
115
  }
112
- if (!doNotCountHasLogged) this.hasBeenLogged = true
116
+
117
+ // THIS is used to access error as object
118
+ this.code = code || 500
119
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !isset(this.options.code))) delete this.code
120
+ this.errorDescription = {
121
+ msg: this.msg,
122
+ code,
123
+ ressource,
124
+ ...extraInfos,
125
+ }
126
+
127
+ this.logs = errorLogs
128
+ }
129
+ log() {
130
+ if (!this.hasBeenLogged) this.logs.forEach(errLine => C.error(false, errLine))
131
+ this.hasBeenLogged = true
113
132
  }
114
133
  toString() {
115
- return this.log(true)
134
+ return this.logs.join('\n')
116
135
  }
117
- }
118
-
119
- function logErr(noStackTrace: boolean, ...params: any[]) {
120
- if (noStackTrace) C.error(false, ...params)
121
- else C.error(...params)
122
136
  }
package/src/types.ts CHANGED
@@ -11,7 +11,7 @@ export type ErrorOptions = {
11
11
  code?: number
12
12
  doNotDisplayCode?: boolean
13
13
  notifyOnSlackChannel?: boolean
14
- extraInfosRenderer?: (extraInfosObj: ObjectGeneric) => void
14
+ extraInfosRenderer?: (extraInfosObj: ObjectGeneric) => string[]
15
15
  doNotWaitOneFrameForLog?: boolean
16
16
  noStackTrace?: boolean
17
17
  [k: string]: any