@vigneshreddy/cms-sdk 1.0.12 → 1.0.14
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/src/errors/base.d.ts +4 -0
- package/dist/src/errors/base.js +10 -1
- package/dist/src/errors.js +22 -4
- package/package.json +1 -1
|
@@ -20,4 +20,8 @@ export declare class CMSAPIError extends Error {
|
|
|
20
20
|
headers?: Record<string, any>;
|
|
21
21
|
};
|
|
22
22
|
constructor(message: string, statusCode: number, type: string, rawError: any, request?: CMSAPIError["request"], response?: CMSAPIError["response"]);
|
|
23
|
+
toJSON(): {
|
|
24
|
+
statusCode: number;
|
|
25
|
+
message: string;
|
|
26
|
+
};
|
|
23
27
|
}
|
package/dist/src/errors/base.js
CHANGED
|
@@ -12,7 +12,10 @@ class CMSAPIError extends Error {
|
|
|
12
12
|
this.statusCode = statusCode;
|
|
13
13
|
this.type = type;
|
|
14
14
|
this.rawError = rawError;
|
|
15
|
-
this.cause =
|
|
15
|
+
this.cause =
|
|
16
|
+
rawError instanceof Error || typeof rawError === "string"
|
|
17
|
+
? rawError
|
|
18
|
+
: undefined;
|
|
16
19
|
this.request = request;
|
|
17
20
|
this.response = response;
|
|
18
21
|
// Ensure proper prototype chain in transpiled output
|
|
@@ -35,5 +38,11 @@ class CMSAPIError extends Error {
|
|
|
35
38
|
// it will just remain enumerable.
|
|
36
39
|
}
|
|
37
40
|
}
|
|
41
|
+
toJSON() {
|
|
42
|
+
return {
|
|
43
|
+
statusCode: this.statusCode,
|
|
44
|
+
message: this.message,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
38
47
|
}
|
|
39
48
|
exports.CMSAPIError = CMSAPIError;
|
package/dist/src/errors.js
CHANGED
|
@@ -29,6 +29,12 @@ function handleApiError(error, request, response) {
|
|
|
29
29
|
if (error instanceof base_1.CMSAPIError) {
|
|
30
30
|
throw error; // preserve original error metadata/type
|
|
31
31
|
}
|
|
32
|
+
const isHtmlResponse = (data) => {
|
|
33
|
+
if (typeof data !== "string")
|
|
34
|
+
return false;
|
|
35
|
+
const trimmed = data.trim().toLowerCase();
|
|
36
|
+
return trimmed.startsWith("<!doctype") || trimmed.startsWith("<html") || trimmed.startsWith("<!-");
|
|
37
|
+
};
|
|
32
38
|
const getBackendErrorMessage = (data) => {
|
|
33
39
|
var _a;
|
|
34
40
|
const directMessage = typeof (data === null || data === void 0 ? void 0 : data.message) === "string" ? data.message.trim() : "";
|
|
@@ -38,6 +44,10 @@ function handleApiError(error, request, response) {
|
|
|
38
44
|
if (nestedMessage)
|
|
39
45
|
return nestedMessage;
|
|
40
46
|
if (Array.isArray(data === null || data === void 0 ? void 0 : data.errors)) {
|
|
47
|
+
const firstObjectErrorCode = data.errors.find((item) => typeof (item === null || item === void 0 ? void 0 : item.code) === "string" && item.code.trim());
|
|
48
|
+
if (firstObjectErrorCode) {
|
|
49
|
+
return firstObjectErrorCode.code.trim();
|
|
50
|
+
}
|
|
41
51
|
const firstStringError = data.errors.find((item) => typeof item === "string" && item.trim());
|
|
42
52
|
if (typeof firstStringError === "string") {
|
|
43
53
|
return firstStringError.trim();
|
|
@@ -46,8 +56,13 @@ function handleApiError(error, request, response) {
|
|
|
46
56
|
if (firstObjectErrorMessage) {
|
|
47
57
|
return firstObjectErrorMessage.message.trim();
|
|
48
58
|
}
|
|
59
|
+
const firstObjectErrorText = data.errors.find((item) => typeof (item === null || item === void 0 ? void 0 : item.errorMessage) === "string" && item.errorMessage.trim());
|
|
60
|
+
if (firstObjectErrorText) {
|
|
61
|
+
return firstObjectErrorText.errorMessage.trim();
|
|
62
|
+
}
|
|
49
63
|
}
|
|
50
|
-
|
|
64
|
+
// Ignore HTML responses (e.g., 404 pages)
|
|
65
|
+
if (typeof data === "string" && data.trim() && !isHtmlResponse(data)) {
|
|
51
66
|
return data.trim();
|
|
52
67
|
}
|
|
53
68
|
return undefined;
|
|
@@ -70,16 +85,19 @@ function handleApiError(error, request, response) {
|
|
|
70
85
|
if (errorWithResponse) {
|
|
71
86
|
const res = errorWithResponse;
|
|
72
87
|
const statusCode = (_c = (_b = res.status) !== null && _b !== void 0 ? _b : res.statusCode) !== null && _c !== void 0 ? _c : 500;
|
|
73
|
-
const
|
|
88
|
+
const isHtml = isHtmlResponse(res.data);
|
|
89
|
+
const backendMessage = isHtml ? undefined : getBackendErrorMessage(res.data);
|
|
74
90
|
const fallbackMessage = `CMS API Error: ${statusCode} - ${(_d = res.statusText) !== null && _d !== void 0 ? _d : "Unknown API Error"}`;
|
|
75
91
|
const message = backendMessage !== null && backendMessage !== void 0 ? backendMessage : fallbackMessage;
|
|
92
|
+
// Don't pass HTML blobs as rawError
|
|
93
|
+
const rawError = isHtml ? undefined : ((_e = res.data) !== null && _e !== void 0 ? _e : error);
|
|
76
94
|
// Prefer typed error subclasses when possible
|
|
77
|
-
const typedError = (0, specific_1.createSpecificError)(statusCode, message,
|
|
95
|
+
const typedError = (0, specific_1.createSpecificError)(statusCode, message, rawError);
|
|
78
96
|
typedError.request = sanitizeRequest(request !== null && request !== void 0 ? request : error === null || error === void 0 ? void 0 : error.request);
|
|
79
97
|
typedError.response = {
|
|
80
98
|
status: statusCode,
|
|
81
99
|
statusText: res.statusText || "",
|
|
82
|
-
data: res.data,
|
|
100
|
+
data: isHtml ? undefined : res.data,
|
|
83
101
|
headers: res.headers,
|
|
84
102
|
};
|
|
85
103
|
throw typedError;
|