@gymspace/evolution 1.2.4 → 1.3.0

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/index.js CHANGED
@@ -34,8 +34,9 @@ var EvolutionApiError = class _EvolutionApiError extends Error {
34
34
  this.message = finalMessage;
35
35
  this.statusCode = statusCode;
36
36
  this.details = cause;
37
- if (Error.captureStackTrace) {
38
- Error.captureStackTrace(this, _EvolutionApiError);
37
+ const ErrorConstructor = Error;
38
+ if (typeof ErrorConstructor.captureStackTrace === "function") {
39
+ ErrorConstructor.captureStackTrace(this, _EvolutionApiError);
39
40
  }
40
41
  }
41
42
  /**
@@ -49,18 +50,18 @@ var EvolutionApiError = class _EvolutionApiError extends Error {
49
50
  if (this.details && typeof this.details === "object") {
50
51
  const details = this.details;
51
52
  const relevantDetails = [];
52
- if (details.url) {
53
+ if (typeof details.url === "string") {
53
54
  relevantDetails.push(`URL: ${details.url}`);
54
55
  }
55
- if (details.method) {
56
+ if (typeof details.method === "string") {
56
57
  relevantDetails.push(`Method: ${details.method}`);
57
58
  }
58
59
  if (details.response && typeof details.response === "object") {
59
60
  const response = details.response;
60
- if (response.error && response.error !== this.message) {
61
+ if (typeof response.error === "string" && response.error !== this.message) {
61
62
  relevantDetails.push(`Server Error: ${response.error}`);
62
63
  }
63
- if (response.message && response.message !== this.message) {
64
+ if (typeof response.message === "string" && response.message !== this.message) {
64
65
  relevantDetails.push(`Server Message: ${response.message}`);
65
66
  }
66
67
  }
@@ -84,6 +85,17 @@ var EvolutionApiError = class _EvolutionApiError extends Error {
84
85
  };
85
86
  }
86
87
  };
88
+ function getNestedProperty(obj, path) {
89
+ let current = obj;
90
+ for (const key of path) {
91
+ if (current && typeof current === "object" && key in current) {
92
+ current = current[key];
93
+ } else {
94
+ return void 0;
95
+ }
96
+ }
97
+ return current;
98
+ }
87
99
  function extractErrorMessage(response) {
88
100
  if (!response) {
89
101
  return null;
@@ -94,45 +106,93 @@ function extractErrorMessage(response) {
94
106
  if (typeof response === "object" && response !== null) {
95
107
  try {
96
108
  const errorObj = response;
97
- const messagePaths = [
109
+ const checkPaths = [
98
110
  // Evolution API specific nested structure (most common)
99
- errorObj.response?.response?.message?.[0],
100
- errorObj.response?.response?.error,
101
- errorObj.response?.response?.description,
111
+ () => {
112
+ const val = getNestedProperty(errorObj, ["response", "response", "message"]);
113
+ if (Array.isArray(val) && val.length > 0) return String(val[0]);
114
+ return null;
115
+ },
116
+ () => {
117
+ const val = getNestedProperty(errorObj, ["response", "response", "error"]);
118
+ return typeof val === "string" ? val : null;
119
+ },
120
+ () => {
121
+ const val = getNestedProperty(errorObj, ["response", "response", "description"]);
122
+ return typeof val === "string" ? val : null;
123
+ },
102
124
  // Evolution API first level nested
103
- Array.isArray(errorObj.response?.message) ? errorObj.response.message[0] : null,
104
- errorObj.response?.error,
105
- errorObj.response?.description,
125
+ () => {
126
+ const val = getNestedProperty(errorObj, ["response", "message"]);
127
+ if (Array.isArray(val) && val.length > 0) return String(val[0]);
128
+ return null;
129
+ },
130
+ () => {
131
+ const val = getNestedProperty(errorObj, ["response", "error"]);
132
+ return typeof val === "string" ? val : null;
133
+ },
134
+ () => {
135
+ const val = getNestedProperty(errorObj, ["response", "description"]);
136
+ return typeof val === "string" ? val : null;
137
+ },
106
138
  // Direct error message
107
- Array.isArray(errorObj.message) ? errorObj.message[0] : errorObj.message,
108
- errorObj.error,
109
- errorObj.description,
110
- errorObj.detail,
139
+ () => {
140
+ const val = errorObj.message;
141
+ if (Array.isArray(val) && val.length > 0) return String(val[0]);
142
+ return typeof val === "string" ? val : null;
143
+ },
144
+ () => {
145
+ const val = errorObj.error;
146
+ return typeof val === "string" ? val : null;
147
+ },
148
+ () => {
149
+ const val = errorObj.description;
150
+ return typeof val === "string" ? val : null;
151
+ },
152
+ () => {
153
+ const val = errorObj.detail;
154
+ return typeof val === "string" ? val : null;
155
+ },
111
156
  // Other nested error messages
112
- errorObj.data?.error,
113
- errorObj.data?.message,
157
+ () => {
158
+ const val = getNestedProperty(errorObj, ["data", "error"]);
159
+ return typeof val === "string" ? val : null;
160
+ },
161
+ () => {
162
+ const val = getNestedProperty(errorObj, ["data", "message"]);
163
+ return typeof val === "string" ? val : null;
164
+ },
114
165
  // Array format messages (fallback)
115
- Array.isArray(errorObj.error) ? errorObj.error[0] : null,
116
- Array.isArray(errorObj.errors) ? errorObj.errors[0] : null
117
- ].filter(Boolean);
118
- for (const path of messagePaths) {
119
- if (path && typeof path === "string" && path.length > 0 && path.trim()) {
120
- return path.trim();
166
+ () => {
167
+ const val = errorObj.error;
168
+ if (Array.isArray(val) && val.length > 0) return String(val[0]);
169
+ return null;
170
+ },
171
+ () => {
172
+ const val = errorObj.errors;
173
+ if (Array.isArray(val) && val.length > 0) return String(val[0]);
174
+ return null;
121
175
  }
122
- if (path && typeof path === "object" && path !== null) {
123
- const nestedMessage = extractErrorMessage(path);
124
- if (nestedMessage) {
125
- return nestedMessage;
126
- }
176
+ ];
177
+ for (const checkPath of checkPaths) {
178
+ const result = checkPath();
179
+ if (result && result.trim().length > 0) {
180
+ return result.trim();
127
181
  }
128
182
  }
129
183
  if (errorObj.validation && Array.isArray(errorObj.validation)) {
130
- const validationErrors = errorObj.validation.map((v) => v?.message || v?.error || String(v)).filter(Boolean).join(", ");
184
+ const validationErrors = errorObj.validation.map((v) => {
185
+ if (v && typeof v === "object") {
186
+ const vObj = v;
187
+ return vObj.message || vObj.error || String(v);
188
+ }
189
+ return String(v);
190
+ }).filter(Boolean).join(", ");
131
191
  if (validationErrors) {
132
192
  return `Validation error: ${validationErrors}`;
133
193
  }
134
194
  }
135
- if (errorObj.statusCode && errorObj.statusText) {
195
+ if (typeof errorObj.statusCode === "number" && typeof errorObj.statusText === "string") {
136
196
  return `${errorObj.statusCode}: ${errorObj.statusText}`;
137
197
  }
138
198
  const keys = Object.keys(errorObj);