debug(template): expose Meta error subcode/details, add canal meta

- WhatsAppService::makeRequest: include error_subcode and error_data.details
  in the thrown exception message to surface the real reason for #100 errors
- chat_send_message: log template name/lang/params before sending;
  pass canal+operator_id meta so outgoing templates are tagged correctly

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-03 14:54:10 -05:00
co-authored by Claude Sonnet 4.6
parent f60df038d8
commit a8380ddc81
2 changed files with 15 additions and 11 deletions
+4 -1
View File
@@ -35,7 +35,10 @@ try {
$wa = new WhatsAppService('turnero');
if ($type === 'template') {
$response = $wa->sendTemplateMessage($user['phone_number'], $template, $lang, $params);
error_log("chat_send_message template: name={$template} lang={$lang} params=" . json_encode($params));
$meta = ['canal' => 'turnero'];
if ($operatorId) $meta['operator_id'] = $operatorId;
$response = $wa->sendTemplateMessage($user['phone_number'], $template, $lang, $params, [], null, $meta);
} else {
$extra = ['canal' => 'turnero'];
if ($operatorId) $extra['operator_id'] = $operatorId;
+11 -10
View File
@@ -854,16 +854,17 @@ class WhatsAppService
$decoded = json_decode($response, true);
if ($httpCode >= 400) {
$errorMsg = isset($decoded['error']['message']) ? $decoded['error']['message'] : 'Error desconocido';
// Debug detallado
error_log("WhatsApp API Error Details:");
error_log("- URL: " . $url);
error_log("- HTTP Code: " . $httpCode);
error_log("- Response: " . $response);
error_log("- Token (first 20 chars): " . substr($this->token, 0, 20) . "...");
throw new Exception("Error de WhatsApp API: " . $errorMsg);
$err = $decoded['error'] ?? [];
$errorMsg = $err['message'] ?? 'Error desconocido';
$details = $err['error_data']['details'] ?? ($err['error_data']['description'] ?? null);
$subcode = $err['error_subcode'] ?? null;
error_log("WhatsApp API Error: HTTP={$httpCode} code={$err['code']} sub={$subcode} msg={$errorMsg} details={$details} body=" . substr($response, 0, 400));
$full = $errorMsg;
if ($subcode) $full .= " (sub:{$subcode})";
if ($details) $full .= "{$details}";
throw new Exception("Error de WhatsApp API: " . $full);
}
return $decoded;