From 0dc44c96e8df1731f58efd934864bebdd8097815 Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Fri, 28 Jul 2023 10:25:20 +0100 Subject: [PATCH] standalone: error handler --- css/menubar-standalone.css | 15 ++++++++++ setup/res/index.php.txt | 46 +++++++++++++++++++++++++++++ templates/CRM/common/standalone.tpl | 6 ++++ 3 files changed, 67 insertions(+) diff --git a/css/menubar-standalone.css b/css/menubar-standalone.css index bcf1fb3343..019ff14567 100644 --- a/css/menubar-standalone.css +++ b/css/menubar-standalone.css @@ -1,3 +1,4 @@ +/* The title of this file is menubar-standalone, but the CSS is more general than that. @todo fixme */ @media (min-width: $breakMin) { body.crm-menubar-visible.crm-menubar-over-cms-menu { @@ -30,3 +31,17 @@ .breadcrumb ol li:not(:first-child)::before { content: " \BB "; } + +.standalone-errors { + background: #ffe8e4; + color: #422; + padding: 1rem; + margin-bottom: 1rem; +} + +.standalone-errors code { + color: #004e81; +} +.standalone-errors .backtrace { + font-size: 0.825rem; +} diff --git a/setup/res/index.php.txt b/setup/res/index.php.txt index 31e3af3e07..aea3a5a585 100644 --- a/setup/res/index.php.txt +++ b/setup/res/index.php.txt @@ -64,8 +64,54 @@ $classLoader = implode(DIRECTORY_SEPARATOR, [$civiCorePath, 'CRM', 'Core', 'Clas require_once $classLoader; CRM_Core_ClassLoader::singleton()->register(); +function standaloneErrorHandler( + int $errno, + string $errstr, + ?string $errfile, + ?int $errline, + ?array $errcontext) { + static $handlingError = FALSE; + if ($handlingError) { + throw new \RuntimeException("Died: error was thrown during error handling"); + } + $handlingError = TRUE; + + $trace = ''; + // Might be nice to offer something like this to trace down errors. + $debug = CRM_Core_Config::singleton()->debug; + if ($debug) { + $trace = []; + foreach (array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1) as $item) { + $_ = ''; + if (!empty($item['function'])) { + if (!empty($item['class']) && !empty($item['type'])) { + $_ = htmlspecialchars("$item[class]$item[type]$item[function]() "); + } + else { + $_ = htmlspecialchars("$item[function]() "); + } + } + $_ .= "" . htmlspecialchars($item['file']) . ' line ' . $item['line']; + $trace[] = $_; + } + $trace = '
' . implode("\n", $trace) . '
'; + } + + if (!isset(Civi::$statics[__FUNCTION__])) { + Civi::$statics[__FUNCTION__] = []; + } + Civi::$statics[__FUNCTION__][] = '
  • ' + . htmlspecialchars("$errstr [$errno]\n") . '' . htmlspecialchars($errfile) . " line $errline" + . $trace + . '
  • '; + CRM_Core_Smarty::singleton()->assign('standaloneErrors', implode("\n", Civi::$statics[__FUNCTION__])); + + $handlingError = FALSE; +} + if (file_exists(findStandaloneSettings())) { require_once findStandaloneSettings(); + set_error_handler('standaloneErrorHandler', E_ALL); invoke(); } else { diff --git a/templates/CRM/common/standalone.tpl b/templates/CRM/common/standalone.tpl index 154adfa2f8..3c5b3f4627 100644 --- a/templates/CRM/common/standalone.tpl +++ b/templates/CRM/common/standalone.tpl @@ -38,6 +38,12 @@ {/if} + {if $standaloneErrors} +
    + +
    + {/if} + {if $pageTitle}

    {if $isDeleted}{/if}{$pageTitle}{if $isDeleted}{/if}

    -- 2.25.1