topkat-utils 1.2.16 → 1.2.17

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
@@ -16,6 +16,7 @@ export declare class DescriptiveError extends Error {
16
16
  options: ErrorOptions;
17
17
  hasBeenLogged: boolean;
18
18
  constructor(msg: string, options?: ErrorOptions);
19
- log(doNotCountHasLogged?: boolean): void;
20
- toString(): void;
19
+ parseError(forCli?: boolean): string[];
20
+ log(): void;
21
+ toString(): string;
21
22
  }
@@ -57,8 +57,10 @@ 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 = {}) {
@@ -80,58 +82,58 @@ class DescriptiveError extends Error {
80
82
  const { onError = () => { } } = (0, config_1.configFn)();
81
83
  onError(msg, options);
82
84
  }
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
- }
85
+ parseError(forCli = false) {
86
+ const errorLogs = [];
87
+ 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"]);
88
+ let { code } = this.options;
89
+ const extraInfos = Object.assign(Object.assign({}, extraInfosRaw), (this.options.extraInfos || {}));
90
+ this.code = code || 500;
91
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !(0, isset_1.isset)(this.options.code)))
92
+ delete this.code;
93
+ if (!(0, isset_1.isset)(extraInfos.value) && this.options.hasOwnProperty('value'))
94
+ extraInfos.value = 'undefined';
95
+ if (!(0, isset_1.isset)(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue'))
96
+ extraInfos.gotValue = 'undefined';
97
+ if ((0, isset_1.isset)(ressource)) {
98
+ code = 404;
99
+ if (this.msg === '404')
100
+ this.msg = `Ressource ${ressource} not found`;
101
+ extraInfos.ressource = ressource;
102
+ }
103
+ errorLogs.push(this.msg || this.message);
104
+ if (Object.keys(extraInfos).length > 0)
105
+ extraInfosRenderer(extraInfos);
106
+ if (err) {
107
+ errorLogs.push('== ORIGINAL ERROR ==');
108
+ if (typeof err.parseError === 'function') {
109
+ err.hasBeenLogged = false;
110
+ const logFromOtherErr = err.parseError(forCli);
111
+ errorLogs.push(...logFromOtherErr);
112
112
  }
113
113
  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)));
114
+ errorLogs.push(err.toString());
115
+ if (!noStackTrace && err.stack)
116
+ errorLogs.push(err.stack);
117
+ if (err.extraInfos)
118
+ errorLogs.push(err.extraInfos);
119
+ }
120
+ }
121
+ else {
122
+ if (!noStackTrace) {
123
+ const stackTranceClean = (0, clean_stack_trace_1.cleanStackTrace)(extraInfosRaw.stack || this.stack);
124
+ errorLogs.push(forCli ? logger_utils_1.C.dim(stackTranceClean) : stackTranceClean);
116
125
  }
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
126
  }
123
- if (!doNotCountHasLogged)
124
- this.hasBeenLogged = true;
127
+ return errorLogs;
128
+ }
129
+ log() {
130
+ if (!this.hasBeenLogged)
131
+ this.parseError().forEach(errLine => logger_utils_1.C.error(false, errLine));
132
+ this.hasBeenLogged = true;
125
133
  }
126
134
  toString() {
127
- return this.log(true);
135
+ return this.parseError().join('\n');
128
136
  }
129
137
  }
130
138
  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
139
  //# sourceMappingURL=error-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-utils.js","sourceRoot":"","sources":["../../src/error-utils.ts"],"names":[],"mappings":";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AO,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACvC,CAAC;CACJ;AAxED,4CAwEC"}
@@ -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.17",
4
4
  "type": "commonjs",
5
5
  "types": "index.ts",
6
6
  "main": "dist",
@@ -44,8 +44,10 @@ export async function tryCatch(callback: Function, onErr: Function = () => { })
44
44
 
45
45
 
46
46
  function extraInfosRendererDefault(extraInfos) {
47
- C.error(false, '== EXTRA INFOS ==')
48
- C.error(false, JSON.stringify(extraInfos, null, 2))
47
+ return [
48
+ '== EXTRA INFOS ==',
49
+ JSON.stringify(extraInfos, null, 2)
50
+ ]
49
51
  }
50
52
 
51
53
  export class DescriptiveError extends Error {
@@ -71,52 +73,53 @@ export class DescriptiveError extends Error {
71
73
  const { onError = () => { } } = configFn()
72
74
  onError(msg, options)
73
75
  }
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
- }
76
+ parseError(forCli = false) {
77
+
78
+ const errorLogs: string[] = []
79
+
80
+ const { err, doNotThrow = false, noStackTrace = false, ressource, extraInfosRenderer = extraInfosRendererDefault, notifyOnSlackChannel = false, originalMessage, ...extraInfosRaw } = this.options
81
+ let { code } = this.options
82
+ const extraInfos = { ...extraInfosRaw, ...(this.options.extraInfos || {}) }
88
83
 
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
- }
84
+ this.code = code || 500
85
+ if (this.options.doNotDisplayCode || (this.options.hasOwnProperty('code') && !isset(this.options.code))) delete this.code
86
+
87
+ if (!isset(extraInfos.value) && this.options.hasOwnProperty('value')) extraInfos.value = 'undefined'
88
+ if (!isset(extraInfos.gotValue) && this.options.hasOwnProperty('gotValue')) extraInfos.gotValue = 'undefined'
89
+
90
+ if (isset(ressource)) {
91
+ code = 404
92
+ if (this.msg === '404') this.msg = `Ressource ${ressource} not found`
93
+ extraInfos.ressource = ressource
94
+ }
95
+
96
+ errorLogs.push(this.msg || this.message)
97
+ if (Object.keys(extraInfos).length > 0) extraInfosRenderer(extraInfos)
98
+ if (err) {
99
+ errorLogs.push('== ORIGINAL ERROR ==')
100
+ if (typeof err.parseError === 'function') {
101
+ err.hasBeenLogged = false
102
+ const logFromOtherErr = err.parseError(forCli)
103
+ errorLogs.push(...logFromOtherErr)
100
104
  } else {
101
- if (!noStackTrace) C.error(false, C.dim(cleanStackTrace(extraInfosRaw.stack || this.stack)))
105
+ errorLogs.push(err.toString())
106
+ if (!noStackTrace && err.stack) errorLogs.push(err.stack)
107
+ if (err.extraInfos) errorLogs.push(err.extraInfos)
102
108
  }
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,
109
+ } else {
110
+ if (!noStackTrace) {
111
+ const stackTranceClean = cleanStackTrace(extraInfosRaw.stack || this.stack)
112
+ errorLogs.push(forCli ? C.dim(stackTranceClean) : stackTranceClean)
110
113
  }
111
114
  }
112
- if (!doNotCountHasLogged) this.hasBeenLogged = true
115
+
116
+ return errorLogs
117
+ }
118
+ log() {
119
+ if (!this.hasBeenLogged) this.parseError().forEach(errLine => C.error(false, errLine))
120
+ this.hasBeenLogged = true
113
121
  }
114
122
  toString() {
115
- return this.log(true)
123
+ return this.parseError().join('\n')
116
124
  }
117
- }
118
-
119
- function logErr(noStackTrace: boolean, ...params: any[]) {
120
- if (noStackTrace) C.error(false, ...params)
121
- else C.error(...params)
122
125
  }
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