diff --git a/classes/class-exception.php b/classes/class-exception.php new file mode 100644 index 0000000..195adb1 --- /dev/null +++ b/classes/class-exception.php @@ -0,0 +1,60 @@ +__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() ); + } +} diff --git a/classes/class-ini.php b/classes/class-ini.php index 5e0ad37..48e700c 100644 --- a/classes/class-ini.php +++ b/classes/class-ini.php @@ -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 ); diff --git a/classes/init.php b/classes/init.php index 4688ac8..316ade2 100644 --- a/classes/init.php +++ b/classes/init.php @@ -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(); diff --git a/install.php b/install.php index 8deb306..d4b3bfb 100644 --- a/install.php +++ b/install.php @@ -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.'), '' . dirname( $temp_config_file ) . '/' ) ); @@ -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) {