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
/**
* 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
/**
* 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;
$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.
*