* new OpenSim_Exception class to make sure all Exceptions are logged

This commit is contained in:
Olivier van Helden
2025-01-12 21:04:37 -04:00
parent 53ac99517a
commit d59e7e2804
4 changed files with 78 additions and 19 deletions
+60
View File
@@ -0,0 +1,60 @@
<?php
/**
* OpenSim_Exception class
*
* This class extends the Exception class to force logging of all exceptions.
*
* @package magicoli/opensim-helpers
*/
class OpenSim_Exception extends Exception {
// Properties defined by parent class, for reference:
// protected string $message = "";
// private string $string = "";
// protected int $code;
// protected string $file = "";
// protected int $line;
// private array $trace = [];
// private ?Throwable $previous = null;
public function __construct( $message, $code = 0, Exception $previous = null ) {
parent::__construct( $message, $code, $previous );
error_log( $this->__toString() );
}
// Disabled custom string representation of the exception, it is worst than the default one.
// public function __toString() {
// $prefix = '';
// // return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
// $message = strip_tags( $this->message );
// $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
// if( ! empty( $trace[1] ) ) {
// $class = $trace[1]['class'] ?? '';
// $function = $trace[1]['function'] ?? '';
// }
// if( ! empty( trim ( $class . $function ) ) ) {
// $prefix .= '(' . ( empty( $class ) ? '' : $class . '::' ) . $function . ') ';
// }
// return $prefix . $message;
// }
}
/**
* This is temporary, it's misleading to replace Errors with Exceptions,but it's a way
* to make sure I can replace all new Error() calls with new OpenSim_Exception() calls.
*
* I pledge to check soon, but I spend too much time finetuning the Error catchers, so I
* want to keep a way to switch back fast if needed.
*
* TODO:
* - Test again every use case where OpenSim_Error is used, make sure interrupts happen as expected.
* - Replace all OpenSim_Error calls with OpenSim_Exception.
* - Remove this class.
*/
class OpenSim_Error extends OpenSim_Exception {
public function __construct( $message, $code = 0, Exception $previous = null ) {
parent::__construct( $message, $code, $previous );
// error_log( $this->__toString() );
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ class OpenSim_Ini {
public function __construct( $args ) {
if( empty( $args ) ) {
throw new Error( __FUNCTION__ .'() empty value received');
throw new OpenSim_Error( __FUNCTION__ .'() empty value received');
}
if( is_string( $args ) && file_exists( $args ) ) {
@@ -31,7 +31,7 @@ class OpenSim_Ini {
} elseif( is_array( $args ) ) {
$this->raw_ini_array = $args;
} else {
throw new Error( __CLASS__ .' accepts only string, array or file path value' );
throw new OpenSim_Error( __CLASS__ .' accepts only string, array or file path value' );
}
$this->sanitize_and_parse( $this->raw_ini_array );
+11 -12
View File
@@ -47,6 +47,7 @@ class OpenSim {
}
public function includes() {
require_once( OSHELPERS_DIR . 'classes/class-exception.php' );
require_once( OSHELPERS_DIR . 'includes/functions.php' );
require_once( OSHELPERS_DIR . 'classes/class-locale.php' );
require_once( OSHELPERS_DIR . 'classes/class-ini.php' );
@@ -122,7 +123,7 @@ class OpenSim {
}
}
if ( ! $dir ) {
throw new Error( 'No writable temporary directory found.' );
throw new OpenSim_Error( 'No writable temporary directory found.' );
}
self::$tmp_dir = $dir;
@@ -197,17 +198,6 @@ class OpenSim {
}
self::notify( $message, $type );
$message = strip_tags( $message );
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if( ! empty( $trace[1] ) ) {
$class = $trace[1]['class'] ?? '';
$function = $trace[1]['function'] ?? '';
}
if( ! empty( trim ( $class . $function ) ) ) {
$prefix .= '(' . ( empty( $class ) ? '' : $class . '::' ) . $function . ') ';
}
error_log( $prefix . $message );
}
public static function notify( $message, $type = 'info' ) {
@@ -647,6 +637,15 @@ class OpenSim {
// self::icon( 'arrow-right-square' ),
);
}
public static function is_error( $thing ) {
// Throwable includes Exception and probably other things.
if ( $thing instanceof Throwable ) {
return true;
}
return false;
}
}
$OpenSim = new OpenSim();
+5 -5
View File
@@ -51,7 +51,7 @@ class OpenSim_Install extends OpenSim_Page {
try {
$result = call_user_func( $task['callback'] );
if( ! $result ) {
throw new Error( $task['error'] ?? $callback_name . '() failed.' );
throw new OpenSim_Error( $task['error'] ?? $callback_name . '() failed.' );
}
} catch (Throwable $e) {
$result = false;
@@ -106,7 +106,7 @@ class OpenSim_Install extends OpenSim_Page {
try {
$config = $_SESSION[self::FORM_ID]['config'] ?? null;
if( empty( $config ) ) {
throw new Error( _('No configuration found.') );
throw new OpenSim_Error( _('No configuration found.') );
}
} catch (Throwable $e) {
OpenSim::notify_error( $e );
@@ -191,7 +191,7 @@ class OpenSim_Install extends OpenSim_Page {
try {
$result = file_put_contents($temp_config_file, $php_template);
if ( ! $result ) {
throw new Error( sprintf(
throw new OpenSim_Error( sprintf(
_( 'Error writing temporary file, make sure the web server has read/write permissions to %s directory.'),
'<nobr><code>' . dirname( $temp_config_file ) . '/</code></nobr>'
) );
@@ -247,7 +247,7 @@ class OpenSim_Install extends OpenSim_Page {
// $message =
// OpenSim::notify_error( _('Invalid answer from is_robust_ini_file') );
// Should not happen, is_robust_ini_file should have thrown an error instead of returning false
throw new Error( _('is_robust_ini_file returned an invalid value.') );
throw new OpenSim_Error( _('is_robust_ini_file returned an invalid value.') );
$errors++;
}
// $ini = new OpenSim_Ini( $values['robust_ini_path'] );
@@ -432,7 +432,7 @@ class OpenSim_Install extends OpenSim_Page {
if( $valid === false ) {
// OpenSim::notify_error( _('Invalid answer from is_robust_ini_file') );
// Should not happen, is_robust_ini_file should have thrown an error instead of returning false
throw new Error( _('Invalid answer from is_robust_ini_file()') );
throw new OpenSim_Error( _('Invalid answer from is_robust_ini_file()') );
$errors++;
}
} catch (Throwable $e) {