getMessage()); if (!defined('LOADED_ENV_PATH')) { define('LOADED_ENV_PATH', 'ERROR: ' . $e->getMessage()); } } // 3. Configure Error Reporting based on environment $isDebug = filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN); if ($isDebug) { ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); } else { ini_set('display_errors', '0'); error_reporting(0); } // 4. Global Uncaught Exception Handler (JSON for APIs / Clean HTML for web) set_exception_handler(function (\Throwable $e) { $isDebug = filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN); error_log('[EXCEPTION] ' . get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine()); $isApi = str_starts_with($_SERVER['REQUEST_URI'] ?? '', '/api'); if (!headers_sent()) { http_response_code(500); if ($isApi) { header('Content-Type: application/json; charset=utf-8'); } else { header('Content-Type: text/html; charset=utf-8'); } } if ($isApi) { $body = [ 'status' => 'error', 'message' => $isDebug ? $e->getMessage() : 'حدث خطأ غير متوقع في الخادم', 'debug' => $isDebug ? [ 'exception' => get_class($e), 'file' => $e->getFile(), 'line' => $e->getLine(), ] : null ]; echo json_encode($body, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } else { echo "خطأ في الخادم

⚠️ حدث خطأ في الخادم

" . htmlspecialchars($e->getMessage()) . "

"; } exit(1); });