evolution-api-sdk
Version:
Unofficial SDK for the Evolution Whatsapp API v2
1 lines • 9.46 kB
Source Map (JSON)
{"version":3,"sources":["../../src/api/errors.ts"],"sourcesContent":["export class EvolutionApiError extends Error {\n\tpublic readonly statusCode?: number;\n\tpublic readonly details?: unknown;\n\n\tconstructor(message: string, cause?: unknown, statusCode?: number) {\n\t\tconst extractedMessage = extractErrorMessage(cause);\n\t\tconst finalMessage = extractedMessage || message || \"Unknown error occurred\";\n\n\t\tsuper(finalMessage);\n\n\t\tthis.name = EvolutionApiError.name;\n\t\tthis.message = finalMessage;\n\t\tthis.statusCode = statusCode;\n\t\tthis.details = cause;\n\n\t\t// Maintain proper stack trace\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, EvolutionApiError);\n\t\t}\n\t}\n\n\t/**\n\t * Returns a user-friendly string representation of the error\n\t */\n\ttoString(): string {\n\t\tlet result = `${this.name}: ${this.message}`;\n\t\t\n\t\tif (this.statusCode) {\n\t\t\tresult += ` (${this.statusCode})`;\n\t\t}\n\t\t\n\t\t// Add relevant details without showing [Object ...]\n\t\tif (this.details && typeof this.details === 'object') {\n\t\t\tconst details = this.details as Record<string, any>;\n\t\t\tconst relevantDetails: string[] = [];\n\t\t\t\n\t\t\tif (details.url) {\n\t\t\t\trelevantDetails.push(`URL: ${details.url}`);\n\t\t\t}\n\t\t\t\n\t\t\tif (details.method) {\n\t\t\t\trelevantDetails.push(`Method: ${details.method}`);\n\t\t\t}\n\t\t\t\n\t\t\t// Show response details if they exist and are meaningful\n\t\t\tif (details.response && typeof details.response === 'object') {\n\t\t\t\tconst response = details.response as Record<string, any>;\n\t\t\t\tif (response.error && response.error !== this.message) {\n\t\t\t\t\trelevantDetails.push(`Server Error: ${response.error}`);\n\t\t\t\t}\n\t\t\t\tif (response.message && response.message !== this.message) {\n\t\t\t\t\trelevantDetails.push(`Server Message: ${response.message}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (relevantDetails.length > 0) {\n\t\t\t\tresult += `\\n ${relevantDetails.join('\\n ')}`;\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn result;\n\t}\n\n\t/**\n\t * Returns a JSON representation suitable for logging\n\t */\n\ttoJSON(): object {\n\t\treturn {\n\t\t\tname: this.name,\n\t\t\tmessage: this.message,\n\t\t\tstatusCode: this.statusCode,\n\t\t\tdetails: this.details,\n\t\t\tstack: this.stack,\n\t\t};\n\t}\n}\n\n/**\n * Extracts error message from various Evolution API error response formats\n */\nexport function extractErrorMessage(response: unknown): string | null {\n\tif (!response) {\n\t\treturn null;\n\t}\n\n\t// Handle string responses\n\tif (typeof response === \"string\") {\n\t\treturn response;\n\t}\n\n\t// Handle object responses\n\tif (typeof response === \"object\" && response !== null) {\n\t\tconst errorObj = response as Record<string, any>;\n\n\t\t// Try different common error message paths, prioritizing Evolution API format\n\t\tconst messagePaths = [\n\t\t\t// Evolution API specific nested structure (most common)\n\t\t\terrorObj.response?.response?.message?.[0],\n\t\t\terrorObj.response?.response?.error,\n\t\t\terrorObj.response?.response?.description,\n\t\t\t\n\t\t\t// Evolution API first level nested\n\t\t\tArray.isArray(errorObj.response?.message) ? errorObj.response.message[0] : null,\n\t\t\terrorObj.response?.error,\n\t\t\terrorObj.response?.description,\n\t\t\t\n\t\t\t// Direct error message\n\t\t\tArray.isArray(errorObj.message) ? errorObj.message[0] : errorObj.message,\n\t\t\terrorObj.error,\n\t\t\terrorObj.description,\n\t\t\terrorObj.detail,\n\t\t\t\n\t\t\t// Other nested error messages\n\t\t\terrorObj.data?.error,\n\t\t\terrorObj.data?.message,\n\t\t\t\n\t\t\t// Array format messages (fallback)\n\t\t\tArray.isArray(errorObj.error) ? errorObj.error[0] : null,\n\t\t\tArray.isArray(errorObj.errors) ? errorObj.errors[0] : null,\n\t\t];\n\n\t\tfor (const path of messagePaths) {\n\t\t\tif (typeof path === \"string\" && path.trim()) {\n\t\t\t\treturn path.trim();\n\t\t\t}\n\t\t\t// Handle nested objects in arrays\n\t\t\tif (typeof path === \"object\" && path !== null) {\n\t\t\t\tconst nestedMessage = extractErrorMessage(path);\n\t\t\t\tif (nestedMessage) {\n\t\t\t\t\treturn nestedMessage;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Handle validation errors (common format)\n\t\tif (errorObj.validation && Array.isArray(errorObj.validation)) {\n\t\t\tconst validationErrors = errorObj.validation\n\t\t\t\t.map((v: any) => v.message || v.error || String(v))\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(\", \");\n\t\t\tif (validationErrors) {\n\t\t\t\treturn `Validation error: ${validationErrors}`;\n\t\t\t}\n\t\t}\n\n\t\t// Handle specific Evolution API error patterns\n\t\tif (errorObj.statusCode && errorObj.statusText) {\n\t\t\treturn `${errorObj.statusCode}: ${errorObj.statusText}`;\n\t\t}\n\n\t\t// Last resort: try to stringify the object meaningfully\n\t\tif (Object.keys(errorObj).length > 0) {\n\t\t\ttry {\n\t\t\t\treturn JSON.stringify(errorObj);\n\t\t\t} catch {\n\t\t\t\treturn \"[Complex error object]\";\n\t\t\t}\n\t\t}\n\t}\n\n\treturn null;\n}\n\n/**\n * Legacy error message patterns for specific Evolution API errors\n * Kept for backward compatibility\n */\nconst SpecificErrorPatterns = [\n\t{\n\t\tpattern: (obj: any) => \n\t\t\tobj?.message?.some?.((m: any) => m?.exists === false && m?.jid && m?.number),\n\t\tmessage: \"Provided number is not a valid WhatsApp number\"\n\t},\n\t{\n\t\tpattern: (obj: any) => \n\t\t\tobj?.message?.some?.((m: string) => typeof m === \"string\" && m.includes(\"Media upload failed\")),\n\t\tmessage: \"Media upload failed on all hosts\"\n\t},\n\t{\n\t\tpattern: (obj: any) => \n\t\t\tobj?.message?.some?.((m: string) => typeof m === \"string\" && m.includes(\"No session\")),\n\t\tmessage: \"No session found, try restarting your instance\"\n\t},\n\t{\n\t\tpattern: (obj: any) => \n\t\t\tobj?.message?.some?.((m: string) => typeof m === \"string\" && m.includes(\"AggregateError\")),\n\t\tmessage: \"Connection error occurred\"\n\t},\n\t{\n\t\tpattern: (obj: any) => \n\t\t\tobj?.message?.some?.((m: string) => typeof m === \"string\" && m.includes(\"AxiosError\")),\n\t\tmessage: (obj: any) => obj.message[0] || \"Network error occurred\"\n\t},\n];\n\n/**\n * Enhanced error message extraction with specific pattern matching\n */\nfunction getErrorMessage(response: unknown): string | null {\n\t// First try the general extraction\n\tconst generalMessage = extractErrorMessage(response);\n\tif (generalMessage) {\n\t\treturn generalMessage;\n\t}\n\n\t// Then try specific patterns\n\tif (typeof response === \"object\" && response !== null) {\n\t\tfor (const { pattern, message } of SpecificErrorPatterns) {\n\t\t\ttry {\n\t\t\t\tif (pattern(response)) {\n\t\t\t\t\treturn typeof message === \"string\" ? message : message(response);\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Continue to next pattern if this one fails\n\t\t\t}\n\t\t}\n\t}\n\n\treturn null;\n}\n"],"mappings":";;;;;AAAO,IAAM,oBAAN,MAAM,2BAA0B,MAAM;AAAA,EAI5C,YAAY,SAAiB,OAAiB,YAAqB;AAClE,UAAM,mBAAmB,oBAAoB,KAAK;AAClD,UAAM,eAAe,oBAAoB,WAAW;AAEpD,UAAM,YAAY;AAPnB,wBAAgB;AAChB,wBAAgB;AAQf,SAAK,OAAO,mBAAkB;AAC9B,SAAK,UAAU;AACf,SAAK,aAAa;AAClB,SAAK,UAAU;AAGf,QAAI,MAAM,mBAAmB;AAC5B,YAAM,kBAAkB,MAAM,kBAAiB;AAAA,IAChD;AAAA,EACD;AAAA;AAAA;AAAA;AAAA,EAKA,WAAmB;AAClB,QAAI,SAAS,GAAG,KAAK,IAAI,KAAK,KAAK,OAAO;AAE1C,QAAI,KAAK,YAAY;AACpB,gBAAU,KAAK,KAAK,UAAU;AAAA,IAC/B;AAGA,QAAI,KAAK,WAAW,OAAO,KAAK,YAAY,UAAU;AACrD,YAAM,UAAU,KAAK;AACrB,YAAM,kBAA4B,CAAC;AAEnC,UAAI,QAAQ,KAAK;AAChB,wBAAgB,K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