From 614fcec464f2f16f67ee4418897e080d7daa5722 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 2 Nov 2020 21:54:45 -0800 Subject: [PATCH] CRM_Core_Error::formatFooException - Don't bomb on 'Error' PHP 7 updated the class hierarchy for exceptions. The top-level types for exceptions are now: * class Throwable * class Error extends Throwable * class Exception extends Throwable This patch fixes a problem when logging `Error`s, as in ```php Civi::log()->warning('There was a problem', [ 'exception' => $e ]); ``` Before ------ If `$e` is an `Error`, it fails to log. After ----- It works whether `$e` is an `Error` or `Exception`. See also: https://www.php.net/manual/en/language.errors.php7.php --- CRM/Core/Error.php | 8 ++++---- tests/phpunit/CRM/Core/ErrorTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php index 6b264a944e..4011b3e0af 100644 --- a/CRM/Core/Error.php +++ b/CRM/Core/Error.php @@ -817,11 +817,11 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * Render an exception as HTML string. * - * @param Exception $e + * @param Throwable $e * @return string * printable HTML text */ - public static function formatHtmlException(Exception $e) { + public static function formatHtmlException(Throwable $e) { $msg = ''; // Exception metadata @@ -856,11 +856,11 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * Write details of an exception to the log. * - * @param Exception $e + * @param Throwable $e * @return string * printable plain text */ - public static function formatTextException(Exception $e) { + public static function formatTextException(Throwable $e) { $msg = get_class($e) . ": \"" . $e->getMessage() . "\"\n"; $ei = $e; diff --git a/tests/phpunit/CRM/Core/ErrorTest.php b/tests/phpunit/CRM/Core/ErrorTest.php index c8f21a3171..94ecc5f96f 100644 --- a/tests/phpunit/CRM/Core/ErrorTest.php +++ b/tests/phpunit/CRM/Core/ErrorTest.php @@ -46,6 +46,18 @@ class CRM_Core_ErrorTest extends CiviUnitTestCase { $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_exception/', $msg); } + public function testExceptionLogging() { + $e = new \Exception("the exception"); + Civi::log()->notice('There was an exception!', [ + 'exception' => $e, + ]); + + $e = new Error('the error'); + Civi::log()->notice('There was an error!', [ + 'exception' => $e, + ]); + } + /** * We have two coding conventions for writing to log. Make sure that they work together. * -- 2.25.1