From f9d83e593747cac6f2a253100ca2a17105e5dbdb Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 27 Feb 2020 08:05:38 -0500 Subject: [PATCH] Api4 - Display sql errors in explorer --- CRM/Api4/Page/AJAX.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/CRM/Api4/Page/AJAX.php b/CRM/Api4/Page/AJAX.php index 94f9c329c7..f5042123ae 100644 --- a/CRM/Api4/Page/AJAX.php +++ b/CRM/Api4/Page/AJAX.php @@ -84,13 +84,23 @@ class CRM_Api4_Page_AJAX extends CRM_Core_Page { } catch (Exception $e) { http_response_code(500); - $response = [ - 'error_code' => $e->getCode(), - ]; + $response = []; if (CRM_Core_Permission::check('view debug output')) { + $response['error_code'] = $e->getCode(); $response['error_message'] = $e->getMessage(); - if (!empty($params['debug']) && \Civi::settings()->get('backtrace')) { - $response['debug']['backtrace'] = $e->getTrace(); + if (!empty($params['debug'])) { + if (method_exists($e, 'getUserInfo')) { + $response['debug']['info'] = $e->getUserInfo(); + } + $cause = method_exists($e, 'getCause') ? $e->getCause() : $e; + if ($cause instanceof \DB_Error) { + $response['debug']['db_error'] = \DB::errorMessage($cause->getCode()); + $response['debug']['sql'][] = $cause->getDebugInfo(); + } + if (\Civi::settings()->get('backtrace')) { + // Would prefer getTrace() but that causes json_encode to bomb + $response['debug']['backtrace'] = $e->getTraceAsString(); + } } } } -- 2.25.1