diff --git a/api/version/media-url.php b/api/version/media-url.php index 5cf8126..db3b280 100644 --- a/api/version/media-url.php +++ b/api/version/media-url.php @@ -20,6 +20,30 @@ function media_log($msg) { $resolvedFromGraph = false; $decoded = null; +// Log de petición inicial +media_log("Request: " . ($_SERVER['REQUEST_METHOD'] ?? 'GET') . " " . ($_SERVER['REQUEST_URI'] ?? '') . " GET:" . json_encode($_GET) . " POST:" . json_encode($_POST)); + +// Manejador de excepciones no capturadas -> devolver JSON claro +set_exception_handler(function($e){ + media_log("Uncaught exception: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine()); + http_response_code(500); + header('Content-Type: application/json; charset=utf-8'); + echo json_encode(['success'=>false,'error'=>'Uncaught exception','detail'=>$e->getMessage()]); + exit; +}); + +// Shutdown handler para capturar errores fatales (E_ERROR, E_PARSE, ...) +register_shutdown_function(function(){ + $err = error_get_last(); + if ($err && in_array($err['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + media_log("Fatal error: " . ($err['message'] ?? '') . " in " . ($err['file'] ?? '') . ":" . ($err['line'] ?? '')); + http_response_code(500); + header('Content-Type: application/json; charset=utf-8'); + echo json_encode(['success'=>false,'error'=>'Fatal error','detail'=>$err]); + // no exit; (shutdown) + } +}); + $isGet = $_SERVER['REQUEST_METHOD'] === 'GET'; $accept = $_SERVER['HTTP_ACCEPT'] ?? ''; $wantsJson = strpos($accept, 'application/json') !== false;