@gymspace/evolution 1.2.5 → 1.3.1

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