corexxx 1.0.142 → 1.0.148

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/dist/dassert.d.ts CHANGED
@@ -1,12 +1,41 @@
1
+ /**
2
+ * DError - Extended Error class with error codes and metadata
3
+ *
4
+ * Provides a custom error class that extends Error with:
5
+ * - Error codes for categorization
6
+ * - Error metadata for additional context
7
+ * - Backward compatibility with standard Error
8
+ */
9
+ export declare class DError extends Error {
10
+ code: string;
11
+ metadata?: Record<string, any>;
12
+ timestamp: number;
13
+ constructor(message: string, code?: string, metadata?: Record<string, any>);
14
+ /**
15
+ * Convert error to JSON for logging/serialization
16
+ */
17
+ toJSON(): {
18
+ name: string;
19
+ message: string;
20
+ code: string;
21
+ metadata: Record<string, any> | undefined;
22
+ timestamp: number;
23
+ stack: string | undefined;
24
+ };
25
+ /**
26
+ * Convert error to string representation
27
+ */
28
+ toString(): string;
29
+ }
1
30
  export declare namespace dassert {
2
- function verifyOrThrow(cond: boolean, msg: string): void;
3
- function verifyNotNullAndEmpty(data: any, msg: string): void;
4
- function verifyOrCrash(cond: boolean, msg?: string): void;
5
- function assertNotEmpty(data: string, msg?: string): void;
6
- function getOrThrow(cond: boolean, msg: string): true;
7
- function verifyNotNullAndUndef(data: any, msg: string): void;
8
- function verifyObject(data: any, msg: string): void;
9
- function verifyArray(data: any, msg: string): void;
10
- function verifyString(data: any, msg: string): void;
11
- function verifyValidItem(item: string, arr: string[]): void;
31
+ function verifyOrThrow(cond: boolean, msg: string, code?: string): void;
32
+ function verifyNotNullAndEmpty(data: any, msg: string, code?: string): void;
33
+ function verifyOrCrash(cond: boolean, msg?: string, code?: string): void;
34
+ function assertNotEmpty(data: string, msg?: string, code?: string): void;
35
+ function getOrThrow(cond: boolean, msg: string, code?: string): true;
36
+ function verifyNotNullAndUndef(data: any, msg: string, code?: string): void;
37
+ function verifyObject(data: any, msg: string, code?: string): void;
38
+ function verifyArray(data: any, msg: string, code?: string): void;
39
+ function verifyString(data: any, msg: string, code?: string): void;
40
+ function verifyValidItem(item: string, arr: string[], code?: string): void;
12
41
  }
package/dist/dassert.js CHANGED
@@ -1,87 +1,120 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ /**
3
+ * DError - Extended Error class with error codes and metadata
4
+ *
5
+ * Provides a custom error class that extends Error with:
6
+ * - Error codes for categorization
7
+ * - Error metadata for additional context
8
+ * - Backward compatibility with standard Error
9
+ */
5
10
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.dassert = void 0;
7
- const underscore_1 = __importDefault(require("underscore"));
11
+ exports.dassert = exports.DError = void 0;
12
+ class DError extends Error {
13
+ constructor(message, code = "UNKNOWN_ERROR", metadata) {
14
+ super(message);
15
+ this.name = "DError";
16
+ this.code = code;
17
+ this.metadata = metadata;
18
+ this.timestamp = Date.now();
19
+ // Maintain proper prototype chain
20
+ Object.setPrototypeOf(this, DError.prototype);
21
+ }
22
+ /**
23
+ * Convert error to JSON for logging/serialization
24
+ */
25
+ toJSON() {
26
+ return {
27
+ name: this.name,
28
+ message: this.message,
29
+ code: this.code,
30
+ metadata: this.metadata,
31
+ timestamp: this.timestamp,
32
+ stack: this.stack,
33
+ };
34
+ }
35
+ /**
36
+ * Convert error to string representation
37
+ */
38
+ toString() {
39
+ return `[${this.code}] ${this.message}`;
40
+ }
41
+ }
42
+ exports.DError = DError;
8
43
  var dassert;
9
44
  (function (dassert) {
10
- function verifyOrThrow(cond, msg) {
45
+ function verifyOrThrow(cond, msg, code = "VERIFICATION_FAILED") {
11
46
  if (!cond) {
12
- throw Error(msg);
47
+ throw new DError(msg, code);
13
48
  }
14
49
  }
15
50
  dassert.verifyOrThrow = verifyOrThrow;
16
- function verifyNotNullAndEmpty(data, msg) {
17
- if (underscore_1.default.isNull(data) || underscore_1.default.isUndefined(data) || underscore_1.default.isEmpty(data)) {
18
- throw Error(msg);
51
+ function verifyNotNullAndEmpty(data, msg, code = "NULL_OR_EMPTY_ERROR") {
52
+ if (data == null ||
53
+ data === undefined ||
54
+ (typeof data === "object" && Object.keys(data).length === 0)) {
55
+ throw new DError(msg, code, { data });
19
56
  }
20
57
  // empty string
21
- if (underscore_1.default.isString(data) && data.trim().length == 0) {
22
- throw Error(msg);
58
+ if (typeof data === "string" && data.trim().length === 0) {
59
+ throw new DError(msg, code, { data });
23
60
  }
24
61
  // empty array
25
- if (underscore_1.default.isArray(data) && data.length == 0) {
26
- throw Error(msg);
27
- }
28
- // empty object
29
- if (underscore_1.default.isObject(data) && Object.keys(data).length == 0) {
30
- throw Error(msg);
62
+ if (Array.isArray(data) && data.length === 0) {
63
+ throw new DError(msg, code, { data });
31
64
  }
32
65
  }
33
66
  dassert.verifyNotNullAndEmpty = verifyNotNullAndEmpty;
34
- function verifyOrCrash(cond, msg = "Verify and crash called") {
67
+ function verifyOrCrash(cond, msg = "Verify and crash called", code = "CRASH_ERROR") {
35
68
  if (!cond) {
36
- throw new Error(msg);
69
+ throw new DError(msg, code);
37
70
  }
38
71
  }
39
72
  dassert.verifyOrCrash = verifyOrCrash;
40
- function assertNotEmpty(data, msg = "Data is empty which I am not expecting") {
41
- if (data == null || data == undefined || data.length == 0) {
42
- throw new Error(msg);
73
+ function assertNotEmpty(data, msg = "Data is empty which I am not expecting", code = "EMPTY_DATA_ERROR") {
74
+ if (data == null || data === undefined || data.length === 0) {
75
+ throw new DError(msg, code, { data });
43
76
  }
44
77
  }
45
78
  dassert.assertNotEmpty = assertNotEmpty;
46
- function getOrThrow(cond, msg) {
79
+ function getOrThrow(cond, msg, code = "GET_OR_THROW_ERROR") {
47
80
  if (!cond) {
48
- throw Error(msg);
81
+ throw new DError(msg, code);
49
82
  }
50
83
  else {
51
84
  return cond;
52
85
  }
53
86
  }
54
87
  dassert.getOrThrow = getOrThrow;
55
- function verifyNotNullAndUndef(data, msg) {
88
+ function verifyNotNullAndUndef(data, msg, code = "NULL_OR_UNDEFINED_ERROR") {
56
89
  if (data == null || data === undefined) {
57
- throw Error(msg);
90
+ throw new DError(msg, code, { data });
58
91
  }
59
92
  }
60
93
  dassert.verifyNotNullAndUndef = verifyNotNullAndUndef;
61
- function verifyObject(data, msg) {
62
- if (underscore_1.default.isArray(data)) {
63
- throw Error(msg);
94
+ function verifyObject(data, msg, code = "NOT_OBJECT_ERROR") {
95
+ if (Array.isArray(data)) {
96
+ throw new DError(msg, code, { data, type: "array" });
64
97
  }
65
- if (!underscore_1.default.isObject(data)) {
66
- throw Error(msg);
98
+ if (typeof data !== "object" || data === null) {
99
+ throw new DError(msg, code, { data, type: typeof data });
67
100
  }
68
101
  }
69
102
  dassert.verifyObject = verifyObject;
70
- function verifyArray(data, msg) {
71
- if (!underscore_1.default.isArray(data)) {
72
- throw Error(msg);
103
+ function verifyArray(data, msg, code = "NOT_ARRAY_ERROR") {
104
+ if (!Array.isArray(data)) {
105
+ throw new DError(msg, code, { data, type: typeof data });
73
106
  }
74
107
  }
75
108
  dassert.verifyArray = verifyArray;
76
- function verifyString(data, msg) {
77
- if (!underscore_1.default.isString(data)) {
78
- throw Error(msg);
109
+ function verifyString(data, msg, code = "NOT_STRING_ERROR") {
110
+ if (typeof data !== "string") {
111
+ throw new DError(msg, code, { data, type: typeof data });
79
112
  }
80
113
  }
81
114
  dassert.verifyString = verifyString;
82
- function verifyValidItem(item, arr) {
83
- if (arr.indexOf(item) == -1) {
84
- throw Error(`Invalid value <${item}>, The input value just be belongs to ${JSON.stringify(arr)}`);
115
+ function verifyValidItem(item, arr, code = "INVALID_ITEM_ERROR") {
116
+ if (!arr.includes(item)) {
117
+ throw new DError(`Invalid value <${item}>, The input value must belong to ${JSON.stringify(arr)}`, code, { item, validItems: arr });
85
118
  }
86
119
  }
87
120
  dassert.verifyValidItem = verifyValidItem;
@@ -1 +1 @@
1
- {"version":3,"file":"dassert.js","sourceRoot":"","sources":["../src/dassert.ts"],"names":[],"mappings":";;;;;;AAAA,4DAA2B;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@ -7,12 +7,15 @@
7
7
  * - All responses are checked for HTTP success
8
8
  * - JSON responses are parsed and returned as objects
9
9
  * - SimpleStore helpers for common API patterns
10
+ * - Detailed error handling for network issues
10
11
  */
11
12
  import { AxiosRequestConfig } from "axios";
13
+ import { DError } from "./dassert";
12
14
  import { TObject } from "./dtypes";
13
- export declare class NetworkError<T = any> extends Error {
15
+ export declare class NetworkError<T = any> extends DError {
14
16
  data?: T;
15
- constructor(message: string, data?: T);
17
+ statusCode?: number;
18
+ constructor(message: string, code?: string, data?: T, statusCode?: number);
16
19
  }
17
20
  export declare namespace dnetwork {
18
21
  /**
package/dist/dnetwork.js CHANGED
@@ -8,6 +8,7 @@
8
8
  * - All responses are checked for HTTP success
9
9
  * - JSON responses are parsed and returned as objects
10
10
  * - SimpleStore helpers for common API patterns
11
+ * - Detailed error handling for network issues
11
12
  */
12
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -27,26 +28,80 @@ const axios_1 = __importDefault(require("axios"));
27
28
  const dassert_1 = require("./dassert");
28
29
  const dlog_1 = require("./dlog");
29
30
  // Custom error class for network-related errors
30
- class NetworkError extends Error {
31
- constructor(message, data) {
32
- super(message);
31
+ class NetworkError extends dassert_1.DError {
32
+ constructor(message, code = "NETWORK_ERROR", data, statusCode) {
33
+ super(message, code, { data, statusCode });
33
34
  this.name = "NetworkError";
34
35
  this.data = data;
35
- Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
36
+ this.statusCode = statusCode;
37
+ Object.setPrototypeOf(this, new.target.prototype);
36
38
  }
37
39
  }
38
40
  exports.NetworkError = NetworkError;
39
41
  var dnetwork;
40
42
  (function (dnetwork) {
43
+ /**
44
+ * Helper function to categorize and handle axios errors
45
+ */
46
+ function handleAxiosError(error, url) {
47
+ var _a, _b, _c;
48
+ let code = "NETWORK_ERROR";
49
+ let message = "Unknown network error";
50
+ if (error.code === "ECONNABORTED") {
51
+ code = "TIMEOUT_ERROR";
52
+ message = `Request timeout. The server took too long to respond.`;
53
+ }
54
+ else if (error.code === "ENOTFOUND" || error.code === "ECONNREFUSED") {
55
+ code = "SERVER_UNREACHABLE";
56
+ message = `Server unreachable. Check your internet connection or DNS settings.`;
57
+ }
58
+ else if (error.code === "ENETUNREACH") {
59
+ code = "NO_INTERNET";
60
+ message = `No internet connection. Unable to reach ${url}.`;
61
+ }
62
+ else if (error.response && error.response.status >= 500) {
63
+ code = "SERVER_ERROR_5XX";
64
+ message = `Server error (${error.response.status}) : ${error.response.statusText}`;
65
+ }
66
+ else if (error.response && error.response.status === 404) {
67
+ code = "NOT_FOUND";
68
+ message = `Resource not found (404) `;
69
+ }
70
+ else if (error.response && error.response.status === 403) {
71
+ code = "FORBIDDEN";
72
+ message = `Access forbidden (403) `;
73
+ }
74
+ else if (error.message === "Network Error") {
75
+ code = "NETWORK_UNREACHABLE";
76
+ message = `Network is unreachable `;
77
+ }
78
+ else {
79
+ message = error.message || "Unknown error occurred";
80
+ }
81
+ dlog_1.dlog.e(`[Network Error] ${message}`, {
82
+ url,
83
+ code,
84
+ status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
85
+ });
86
+ throw new NetworkError(message, code, (_b = error.response) === null || _b === void 0 ? void 0 : _b.data, (_c = error.response) === null || _c === void 0 ? void 0 : _c.status);
87
+ }
41
88
  /**
42
89
  * Perform a GET request and return the response data.
43
90
  */
44
91
  function get(url, config) {
45
92
  return __awaiter(this, void 0, void 0, function* () {
46
- dlog_1.dlog.d(`GET ${url}`);
47
- const result = yield axios_1.default.get(url, config);
48
- dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `GET request to URL ${url} failed with status ${result.status}`);
49
- return result.data;
93
+ try {
94
+ dlog_1.dlog.d(`GET ${url}`);
95
+ const result = yield axios_1.default.get(url, config);
96
+ dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `GET request to URL ${url} failed with status ${result.status}`, "HTTP_STATUS_ERROR");
97
+ return result.data;
98
+ }
99
+ catch (error) {
100
+ if (axios_1.default.isAxiosError(error)) {
101
+ handleAxiosError(error, url);
102
+ }
103
+ throw error;
104
+ }
50
105
  });
51
106
  }
52
107
  dnetwork.get = get;
@@ -55,15 +110,23 @@ var dnetwork;
55
110
  */
56
111
  function getJson(url, config) {
57
112
  return __awaiter(this, void 0, void 0, function* () {
58
- const res = yield get(url, config);
59
- if (typeof res === "object" && res !== null) {
60
- return res;
61
- }
62
113
  try {
63
- return JSON.parse(res);
114
+ const res = yield get(url, config);
115
+ if (typeof res === "object" && res !== null) {
116
+ return res;
117
+ }
118
+ try {
119
+ return JSON.parse(res);
120
+ }
121
+ catch (_a) {
122
+ throw new NetworkError("Response is not valid JSON", "JSON_PARSE_ERROR", res);
123
+ }
64
124
  }
65
- catch (_a) {
66
- throw new NetworkError("Response is not valid JSON", res);
125
+ catch (error) {
126
+ if (axios_1.default.isAxiosError(error)) {
127
+ handleAxiosError(error, url);
128
+ }
129
+ throw error;
67
130
  }
68
131
  });
69
132
  }
@@ -73,10 +136,18 @@ var dnetwork;
73
136
  */
74
137
  function post(url, data, config) {
75
138
  return __awaiter(this, void 0, void 0, function* () {
76
- dlog_1.dlog.d(`POST ${url}`, data);
77
- const result = yield axios_1.default.post(url, data, config);
78
- dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `POST request to URL ${url} failed with status ${result.status}`);
79
- return result.data;
139
+ try {
140
+ dlog_1.dlog.d(`POST ${url}`, data);
141
+ const result = yield axios_1.default.post(url, data, config);
142
+ dassert_1.dassert.verifyOrThrow(result.status >= 200 && result.status < 300, `POST request to URL ${url} failed with status ${result.status}`, "HTTP_STATUS_ERROR");
143
+ return result.data;
144
+ }
145
+ catch (error) {
146
+ if (axios_1.default.isAxiosError(error)) {
147
+ handleAxiosError(error, url);
148
+ }
149
+ throw error;
150
+ }
80
151
  });
81
152
  }
82
153
  dnetwork.post = post;
@@ -85,15 +156,23 @@ var dnetwork;
85
156
  */
86
157
  function postJson(url, data, config) {
87
158
  return __awaiter(this, void 0, void 0, function* () {
88
- const res = yield post(url, data, config);
89
- if (typeof res === "object" && res !== null) {
90
- return res;
91
- }
92
159
  try {
93
- return JSON.parse(res);
160
+ const res = yield post(url, data, config);
161
+ if (typeof res === "object" && res !== null) {
162
+ return res;
163
+ }
164
+ try {
165
+ return JSON.parse(res);
166
+ }
167
+ catch (_a) {
168
+ throw new NetworkError("Response is not valid JSON", "JSON_PARSE_ERROR", res);
169
+ }
94
170
  }
95
- catch (_a) {
96
- throw new Error("Response is not valid JSON");
171
+ catch (error) {
172
+ if (axios_1.default.isAxiosError(error)) {
173
+ handleAxiosError(error, url);
174
+ }
175
+ throw error;
97
176
  }
98
177
  });
99
178
  }
@@ -103,16 +182,24 @@ var dnetwork;
103
182
  */
104
183
  function getSimpleStore(url, config) {
105
184
  return __awaiter(this, void 0, void 0, function* () {
106
- dlog_1.dlog.d(`GET ${url}`);
107
- const response = yield axios_1.default.get(url, config);
108
- const jsondata = response.data;
109
- if (jsondata.status === "success") {
110
- dlog_1.dlog.d("fetch success");
111
- return jsondata;
185
+ try {
186
+ dlog_1.dlog.d(`GET ${url}`);
187
+ const response = yield axios_1.default.get(url, config);
188
+ const jsondata = response.data;
189
+ if (jsondata.status === "success") {
190
+ dlog_1.dlog.d("fetch success");
191
+ return jsondata;
192
+ }
193
+ else {
194
+ dlog_1.dlog.e(`GET failed: ${url}`, jsondata);
195
+ throw new NetworkError(jsondata.msg || "SimpleStore request failed", "SIMPLESTORE_ERROR", jsondata);
196
+ }
112
197
  }
113
- else {
114
- dlog_1.dlog.e(`GET failed: ${url}`, jsondata);
115
- throw new NetworkError(jsondata.msg, jsondata);
198
+ catch (error) {
199
+ if (axios_1.default.isAxiosError(error)) {
200
+ handleAxiosError(error, url);
201
+ }
202
+ throw error;
116
203
  }
117
204
  });
118
205
  }
@@ -122,16 +209,24 @@ var dnetwork;
122
209
  */
123
210
  function postSimpleStore(url, data, config) {
124
211
  return __awaiter(this, void 0, void 0, function* () {
125
- dlog_1.dlog.d(`POST ${url}`, data);
126
- const response = yield axios_1.default.post(url, data, config);
127
- const jsondata = response.data;
128
- if (jsondata.status === "success") {
129
- dlog_1.dlog.d("POST success");
130
- return jsondata;
212
+ try {
213
+ dlog_1.dlog.d(`POST ${url}`, data);
214
+ const response = yield axios_1.default.post(url, data, config);
215
+ const jsondata = response.data;
216
+ if (jsondata.status === "success") {
217
+ dlog_1.dlog.d("POST success");
218
+ return jsondata;
219
+ }
220
+ else {
221
+ dlog_1.dlog.e(`POST failed: ${url}`, jsondata);
222
+ throw new NetworkError(jsondata.msg || "SimpleStore request failed", "SIMPLESTORE_ERROR", jsondata);
223
+ }
131
224
  }
132
- else {
133
- dlog_1.dlog.e(`POST failed: ${url}`, jsondata);
134
- throw new NetworkError(jsondata.msg, jsondata);
225
+ catch (error) {
226
+ if (axios_1.default.isAxiosError(error)) {
227
+ handleAxiosError(error, url);
228
+ }
229
+ throw error;
135
230
  }
136
231
  });
137
232
  }
@@ -141,9 +236,17 @@ var dnetwork;
141
236
  */
142
237
  function getAsFakeBrowser(url, config) {
143
238
  return __awaiter(this, void 0, void 0, function* () {
144
- const headers = Object.assign({ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36" }, ((config === null || config === void 0 ? void 0 : config.headers) || {}));
145
- const resp = yield axios_1.default.get(url, Object.assign(Object.assign({}, config), { headers }));
146
- return resp.data;
239
+ try {
240
+ const headers = Object.assign({ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36" }, ((config === null || config === void 0 ? void 0 : config.headers) || {}));
241
+ const resp = yield axios_1.default.get(url, Object.assign(Object.assign({}, config), { headers }));
242
+ return resp.data;
243
+ }
244
+ catch (error) {
245
+ if (axios_1.default.isAxiosError(error)) {
246
+ handleAxiosError(error, url);
247
+ }
248
+ throw error;
249
+ }
147
250
  });
148
251
  }
149
252
  dnetwork.getAsFakeBrowser = getAsFakeBrowser;
@@ -1 +1 @@
1
- {"version":3,"file":"dnetwork.js","sourceRoot":"","sources":["../src/dnetwork.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;;;;;;;;;;AAEH,kDAAkD;AAClD,uCAAoC;AACpC,iCAA8B;AAG9B,gDAAgD;AAChD,MAAa,YAAsB,SAAQ,KAAK;IAG9C,YAAY,OAAe,EAAE,IAAQ;QACnC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,0BAA0B;IAC/E,CAAC;CACF;AATD,oCASC;AAED,IAAiB,QAAQ,CA6HxB;AA7HD,WAAiB,QAAQ;IACvB;;OAEG;IACH,SAAsB,GAAG,CACvB,GAAW,EACX,MAA2B;;YAE3B,WAAI,CAAC,CAAC,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC;YACrB,MAAM,MAAM,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YAC5C,iBAAO,CAAC,aAAa,CACnB,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,MAAM,GAAG,GAAG,EAC3C,sBAAsB,GAAG,uBAAuB,MAAM,CAAC,MAAM,EAAE,CAChE,CAAC;YACF,OAAO,MAAM,CAAC,IAAI,CAAC;QACrB,CAAC;KAAA;IAXqB,YAAG,MAWxB,CAAA;IAED;;OAEG;IACH,SAAsB,OAAO,CAC3B,GAAW,EACX,MAA2B;;YAE3B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YACn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AAC,CAAC;gBAC/B,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;KAAA;IA1BqB,wBAAe,kBA0BpC,CAAA;IAED;;OAEG;IACH,SAAsB,gBAAgB,CACpC,GAAW,EACX,MAA2B;;YAE3B,IAAI,CAAC;gBACH,MAAM,OAAO,mBACX,YAAY,EACV,qHAAqH,IACpH,CAAC,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,KAAI,EAAE,CAAC,CAC3B,CAAC;gBACF,MAAM,IAAI,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,GAAG,kCAAO,MAAM,KAAE,OAAO,IAAG,CAAC;gBAC1D,OAAO,IAAI,CAAC,IAAI,CAAC;YACnB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBAC9B,gBAAgB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;gBAC/B,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;KAAA;IAlBqB,yBAAgB,mBAkBrC,CAAA;AACH,CAAC,EA7OgB,QAAQ,wBAAR,QAAQ,QA6OxB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corexxx",
3
- "version": "1.0.142",
3
+ "version": "1.0.148",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/dassert.ts CHANGED
@@ -1,76 +1,175 @@
1
- import _ from "underscore";
1
+ /**
2
+ * DError - Extended Error class with error codes and metadata
3
+ *
4
+ * Provides a custom error class that extends Error with:
5
+ * - Error codes for categorization
6
+ * - Error metadata for additional context
7
+ * - Backward compatibility with standard Error
8
+ */
9
+
10
+ export class DError extends Error {
11
+ public code: string;
12
+ public metadata?: Record<string, any>;
13
+ public timestamp: number;
14
+
15
+ constructor(
16
+ message: string,
17
+ code: string = "UNKNOWN_ERROR",
18
+ metadata?: Record<string, any>
19
+ ) {
20
+ super(message);
21
+ this.name = "DError";
22
+ this.code = code;
23
+ this.metadata = metadata;
24
+ this.timestamp = Date.now();
25
+
26
+ // Maintain proper prototype chain
27
+ Object.setPrototypeOf(this, DError.prototype);
28
+ }
29
+
30
+ /**
31
+ * Convert error to JSON for logging/serialization
32
+ */
33
+ toJSON() {
34
+ return {
35
+ name: this.name,
36
+ message: this.message,
37
+ code: this.code,
38
+ metadata: this.metadata,
39
+ timestamp: this.timestamp,
40
+ stack: this.stack,
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Convert error to string representation
46
+ */
47
+ toString(): string {
48
+ return `[${this.code}] ${this.message}`;
49
+ }
50
+ }
51
+
2
52
  export namespace dassert {
3
- export function verifyOrThrow(cond: boolean, msg: string) {
53
+ export function verifyOrThrow(
54
+ cond: boolean,
55
+ msg: string,
56
+ code: string = "VERIFICATION_FAILED"
57
+ ) {
4
58
  if (!cond) {
5
- throw Error(msg);
59
+ throw new DError(msg, code);
6
60
  }
7
61
  }
8
- export function verifyNotNullAndEmpty(data: any, msg: string) {
9
- if (_.isNull(data) || _.isUndefined(data) || _.isEmpty(data)) {
10
- throw Error(msg);
62
+
63
+ export function verifyNotNullAndEmpty(
64
+ data: any,
65
+ msg: string,
66
+ code: string = "NULL_OR_EMPTY_ERROR"
67
+ ) {
68
+ if (
69
+ data == null ||
70
+ data === undefined ||
71
+ (typeof data === "object" && Object.keys(data).length === 0)
72
+ ) {
73
+ throw new DError(msg, code, { data });
11
74
  }
12
75
  // empty string
13
- if (_.isString(data) && data.trim().length == 0) {
14
- throw Error(msg);
76
+ if (typeof data === "string" && data.trim().length === 0) {
77
+ throw new DError(msg, code, { data });
15
78
  }
16
79
  // empty array
17
- if (_.isArray(data) && data.length == 0) {
18
- throw Error(msg);
19
- }
20
- // empty object
21
- if (_.isObject(data) && Object.keys(data).length == 0) {
22
- throw Error(msg);
80
+ if (Array.isArray(data) && data.length === 0) {
81
+ throw new DError(msg, code, { data });
23
82
  }
24
83
  }
25
84
 
26
- export function verifyOrCrash(cond: boolean, msg = "Verify and crash called") {
85
+ export function verifyOrCrash(
86
+ cond: boolean,
87
+ msg: string = "Verify and crash called",
88
+ code: string = "CRASH_ERROR"
89
+ ) {
27
90
  if (!cond) {
28
- throw new Error(msg);
91
+ throw new DError(msg, code);
29
92
  }
30
93
  }
31
94
 
32
- export function assertNotEmpty(data: string, msg = "Data is empty which I am not expecting") {
33
- if (data == null || data == undefined || data.length == 0) {
34
- throw new Error(msg);
95
+ export function assertNotEmpty(
96
+ data: string,
97
+ msg: string = "Data is empty which I am not expecting",
98
+ code: string = "EMPTY_DATA_ERROR"
99
+ ) {
100
+ if (data == null || data === undefined || data.length === 0) {
101
+ throw new DError(msg, code, { data });
35
102
  }
36
103
  }
37
- export function getOrThrow(cond: boolean, msg: string) {
104
+
105
+ export function getOrThrow(
106
+ cond: boolean,
107
+ msg: string,
108
+ code: string = "GET_OR_THROW_ERROR"
109
+ ) {
38
110
  if (!cond) {
39
- throw Error(msg);
111
+ throw new DError(msg, code);
40
112
  } else {
41
113
  return cond;
42
114
  }
43
115
  }
44
116
 
45
- export function verifyNotNullAndUndef(data: any, msg: string) {
117
+ export function verifyNotNullAndUndef(
118
+ data: any,
119
+ msg: string,
120
+ code: string = "NULL_OR_UNDEFINED_ERROR"
121
+ ) {
46
122
  if (data == null || data === undefined) {
47
- throw Error(msg);
123
+ throw new DError(msg, code, { data });
48
124
  }
49
125
  }
50
126
 
51
- export function verifyObject(data: any, msg: string) {
52
- if (_.isArray(data)) {
53
- throw Error(msg);
127
+ export function verifyObject(
128
+ data: any,
129
+ msg: string,
130
+ code: string = "NOT_OBJECT_ERROR"
131
+ ) {
132
+ if (Array.isArray(data)) {
133
+ throw new DError(msg, code, { data, type: "array" });
54
134
  }
55
- if (!_.isObject(data)) {
56
- throw Error(msg);
135
+ if (typeof data !== "object" || data === null) {
136
+ throw new DError(msg, code, { data, type: typeof data });
57
137
  }
58
138
  }
59
- export function verifyArray(data: any, msg: string) {
60
- if (!_.isArray(data)) {
61
- throw Error(msg);
139
+
140
+ export function verifyArray(
141
+ data: any,
142
+ msg: string,
143
+ code: string = "NOT_ARRAY_ERROR"
144
+ ) {
145
+ if (!Array.isArray(data)) {
146
+ throw new DError(msg, code, { data, type: typeof data });
62
147
  }
63
148
  }
64
149
 
65
- export function verifyString(data: any, msg: string) {
66
- if (!_.isString(data)) {
67
- throw Error(msg);
150
+ export function verifyString(
151
+ data: any,
152
+ msg: string,
153
+ code: string = "NOT_STRING_ERROR"
154
+ ) {
155
+ if (typeof data !== "string") {
156
+ throw new DError(msg, code, { data, type: typeof data });
68
157
  }
69
158
  }
70
159
 
71
- export function verifyValidItem(item: string, arr: string[]) {
72
- if (arr.indexOf(item) == -1) {
73
- throw Error(`Invalid value <${item}>, The input value just be belongs to ${JSON.stringify(arr)}`);
160
+ export function verifyValidItem(
161
+ item: string,
162
+ arr: string[],
163
+ code: string = "INVALID_ITEM_ERROR"
164
+ ) {
165
+ if (!arr.includes(item)) {
166
+ throw new DError(
167
+ `Invalid value <${item}>, The input value must belong to ${JSON.stringify(
168
+ arr
169
+ )}`,
170
+ code,
171
+ { item, validItems: arr }
172
+ );
74
173
  }
75
174
  }
76
175
  }
package/src/dnetwork.ts CHANGED
@@ -7,40 +7,101 @@
7
7
  * - All responses are checked for HTTP success
8
8
  * - JSON responses are parsed and returned as objects
9
9
  * - SimpleStore helpers for common API patterns
10
+ * - Detailed error handling for network issues
10
11
  */
11
12
 
12
- import axios, { AxiosRequestConfig } from "axios";
13
- import { dassert } from "./dassert";
13
+ import axios, { AxiosError, AxiosRequestConfig } from "axios";
14
+ import { dassert, DError } from "./dassert";
14
15
  import { dlog } from "./dlog";
15
16
  import { TObject } from "./dtypes";
16
17
 
17
18
  // Custom error class for network-related errors
18
- export class NetworkError<T = any> extends Error {
19
+ export class NetworkError<T = any> extends DError {
19
20
  data?: T;
21
+ statusCode?: number;
20
22
 
21
- constructor(message: string, data?: T) {
22
- super(message);
23
+ constructor(
24
+ message: string,
25
+ code: string = "NETWORK_ERROR",
26
+ data?: T,
27
+ statusCode?: number,
28
+ ) {
29
+ super(message, code, { data, statusCode });
23
30
  this.name = "NetworkError";
24
31
  this.data = data;
25
- Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
32
+ this.statusCode = statusCode;
33
+ Object.setPrototypeOf(this, new.target.prototype);
26
34
  }
27
35
  }
28
36
 
29
37
  export namespace dnetwork {
38
+ /**
39
+ * Helper function to categorize and handle axios errors
40
+ */
41
+ function handleAxiosError(error: AxiosError, url: string): never {
42
+ let code = "NETWORK_ERROR";
43
+ let message = "Unknown network error";
44
+
45
+ if (error.code === "ECONNABORTED") {
46
+ code = "TIMEOUT_ERROR";
47
+ message = `Request timeout. The server took too long to respond.`;
48
+ } else if (error.code === "ENOTFOUND" || error.code === "ECONNREFUSED") {
49
+ code = "SERVER_UNREACHABLE";
50
+ message = `Server unreachable. Check your internet connection or DNS settings.`;
51
+ } else if (error.code === "ENETUNREACH") {
52
+ code = "NO_INTERNET";
53
+ message = `No internet connection. Unable to reach ${url}.`;
54
+ } else if (error.response && error.response.status >= 500) {
55
+ code = "SERVER_ERROR_5XX";
56
+ message = `Server error (${error.response.status}) : ${error.response.statusText}`;
57
+ } else if (error.response && error.response.status === 404) {
58
+ code = "NOT_FOUND";
59
+ message = `Resource not found (404) `;
60
+ } else if (error.response && error.response.status === 403) {
61
+ code = "FORBIDDEN";
62
+ message = `Access forbidden (403) `;
63
+ } else if (error.message === "Network Error") {
64
+ code = "NETWORK_UNREACHABLE";
65
+ message = `Network is unreachable `;
66
+ } else {
67
+ message = error.message || "Unknown error occurred";
68
+ }
69
+
70
+ dlog.e(`[Network Error] ${message}`, {
71
+ url,
72
+ code,
73
+ status: error.response?.status,
74
+ });
75
+ throw new NetworkError(
76
+ message,
77
+ code,
78
+ error.response?.data,
79
+ error.response?.status,
80
+ );
81
+ }
82
+
30
83
  /**
31
84
  * Perform a GET request and return the response data.
32
85
  */
33
86
  export async function get(
34
87
  url: string,
35
- config?: AxiosRequestConfig
88
+ config?: AxiosRequestConfig,
36
89
  ): Promise<any> {
37
- dlog.d(`GET ${url}`);
38
- const result = await axios.get(url, config);
39
- dassert.verifyOrThrow(
40
- result.status >= 200 && result.status < 300,
41
- `GET request to URL ${url} failed with status ${result.status}`
42
- );
43
- return result.data;
90
+ try {
91
+ dlog.d(`GET ${url}`);
92
+ const result = await axios.get(url, config);
93
+ dassert.verifyOrThrow(
94
+ result.status >= 200 && result.status < 300,
95
+ `GET request to URL ${url} failed with status ${result.status}`,
96
+ "HTTP_STATUS_ERROR",
97
+ );
98
+ return result.data;
99
+ } catch (error) {
100
+ if (axios.isAxiosError(error)) {
101
+ handleAxiosError(error, url);
102
+ }
103
+ throw error;
104
+ }
44
105
  }
45
106
 
46
107
  /**
@@ -48,16 +109,27 @@ export namespace dnetwork {
48
109
  */
49
110
  export async function getJson(
50
111
  url: string,
51
- config?: AxiosRequestConfig
112
+ config?: AxiosRequestConfig,
52
113
  ): Promise<object> {
53
- const res = await get(url, config);
54
- if (typeof res === "object" && res !== null) {
55
- return res;
56
- }
57
114
  try {
58
- return JSON.parse(res);
59
- } catch {
60
- throw new NetworkError("Response is not valid JSON", res);
115
+ const res = await get(url, config);
116
+ if (typeof res === "object" && res !== null) {
117
+ return res;
118
+ }
119
+ try {
120
+ return JSON.parse(res);
121
+ } catch {
122
+ throw new NetworkError(
123
+ "Response is not valid JSON",
124
+ "JSON_PARSE_ERROR",
125
+ res,
126
+ );
127
+ }
128
+ } catch (error) {
129
+ if (axios.isAxiosError(error)) {
130
+ handleAxiosError(error, url);
131
+ }
132
+ throw error;
61
133
  }
62
134
  }
63
135
 
@@ -67,15 +139,23 @@ export namespace dnetwork {
67
139
  export async function post(
68
140
  url: string,
69
141
  data: any,
70
- config?: AxiosRequestConfig
142
+ config?: AxiosRequestConfig,
71
143
  ): Promise<any> {
72
- dlog.d(`POST ${url}`, data);
73
- const result = await axios.post(url, data, config);
74
- dassert.verifyOrThrow(
75
- result.status >= 200 && result.status < 300,
76
- `POST request to URL ${url} failed with status ${result.status}`
77
- );
78
- return result.data;
144
+ try {
145
+ dlog.d(`POST ${url}`, data);
146
+ const result = await axios.post(url, data, config);
147
+ dassert.verifyOrThrow(
148
+ result.status >= 200 && result.status < 300,
149
+ `POST request to URL ${url} failed with status ${result.status}`,
150
+ "HTTP_STATUS_ERROR",
151
+ );
152
+ return result.data;
153
+ } catch (error) {
154
+ if (axios.isAxiosError(error)) {
155
+ handleAxiosError(error, url);
156
+ }
157
+ throw error;
158
+ }
79
159
  }
80
160
 
81
161
  /**
@@ -84,16 +164,27 @@ export namespace dnetwork {
84
164
  export async function postJson(
85
165
  url: string,
86
166
  data: TObject,
87
- config?: AxiosRequestConfig
167
+ config?: AxiosRequestConfig,
88
168
  ): Promise<object> {
89
- const res = await post(url, data, config);
90
- if (typeof res === "object" && res !== null) {
91
- return res;
92
- }
93
169
  try {
94
- return JSON.parse(res);
95
- } catch {
96
- throw new Error("Response is not valid JSON");
170
+ const res = await post(url, data, config);
171
+ if (typeof res === "object" && res !== null) {
172
+ return res;
173
+ }
174
+ try {
175
+ return JSON.parse(res);
176
+ } catch {
177
+ throw new NetworkError(
178
+ "Response is not valid JSON",
179
+ "JSON_PARSE_ERROR",
180
+ res,
181
+ );
182
+ }
183
+ } catch (error) {
184
+ if (axios.isAxiosError(error)) {
185
+ handleAxiosError(error, url);
186
+ }
187
+ throw error;
97
188
  }
98
189
  }
99
190
 
@@ -102,17 +193,28 @@ export namespace dnetwork {
102
193
  */
103
194
  export async function getSimpleStore(
104
195
  url: string,
105
- config?: AxiosRequestConfig
196
+ config?: AxiosRequestConfig,
106
197
  ): Promise<any> {
107
- dlog.d(`GET ${url}`);
108
- const response = await axios.get(url, config);
109
- const jsondata = response.data;
110
- if (jsondata.status === "success") {
111
- dlog.d("fetch success");
112
- return jsondata;
113
- } else {
114
- dlog.e(`GET failed: ${url}`, jsondata);
115
- throw new NetworkError(jsondata.msg, jsondata);
198
+ try {
199
+ dlog.d(`GET ${url}`);
200
+ const response = await axios.get(url, config);
201
+ const jsondata = response.data;
202
+ if (jsondata.status === "success") {
203
+ dlog.d("fetch success");
204
+ return jsondata;
205
+ } else {
206
+ dlog.e(`GET failed: ${url}`, jsondata);
207
+ throw new NetworkError(
208
+ jsondata.msg || "SimpleStore request failed",
209
+ "SIMPLESTORE_ERROR",
210
+ jsondata,
211
+ );
212
+ }
213
+ } catch (error) {
214
+ if (axios.isAxiosError(error)) {
215
+ handleAxiosError(error, url);
216
+ }
217
+ throw error;
116
218
  }
117
219
  }
118
220
 
@@ -122,17 +224,28 @@ export namespace dnetwork {
122
224
  export async function postSimpleStore(
123
225
  url: string,
124
226
  data: TObject,
125
- config?: AxiosRequestConfig
227
+ config?: AxiosRequestConfig,
126
228
  ): Promise<any> {
127
- dlog.d(`POST ${url}`, data);
128
- const response = await axios.post(url, data, config);
129
- const jsondata = response.data;
130
- if (jsondata.status === "success") {
131
- dlog.d("POST success");
132
- return jsondata;
133
- } else {
134
- dlog.e(`POST failed: ${url}`, jsondata);
135
- throw new NetworkError(jsondata.msg, jsondata);
229
+ try {
230
+ dlog.d(`POST ${url}`, data);
231
+ const response = await axios.post(url, data, config);
232
+ const jsondata = response.data;
233
+ if (jsondata.status === "success") {
234
+ dlog.d("POST success");
235
+ return jsondata;
236
+ } else {
237
+ dlog.e(`POST failed: ${url}`, jsondata);
238
+ throw new NetworkError(
239
+ jsondata.msg || "SimpleStore request failed",
240
+ "SIMPLESTORE_ERROR",
241
+ jsondata,
242
+ );
243
+ }
244
+ } catch (error) {
245
+ if (axios.isAxiosError(error)) {
246
+ handleAxiosError(error, url);
247
+ }
248
+ throw error;
136
249
  }
137
250
  }
138
251
 
@@ -141,14 +254,21 @@ export namespace dnetwork {
141
254
  */
142
255
  export async function getAsFakeBrowser(
143
256
  url: string,
144
- config?: AxiosRequestConfig
257
+ config?: AxiosRequestConfig,
145
258
  ): Promise<any> {
146
- const headers = {
147
- "user-agent":
148
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36",
149
- ...(config?.headers || {}),
150
- };
151
- const resp = await axios.get(url, { ...config, headers });
152
- return resp.data;
259
+ try {
260
+ const headers = {
261
+ "user-agent":
262
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36",
263
+ ...(config?.headers || {}),
264
+ };
265
+ const resp = await axios.get(url, { ...config, headers });
266
+ return resp.data;
267
+ } catch (error) {
268
+ if (axios.isAxiosError(error)) {
269
+ handleAxiosError(error, url);
270
+ }
271
+ throw error;
272
+ }
153
273
  }
154
274
  }