From d544365399432eccc2a4cdeef87c640adccc67b3 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 22 Jul 2025 20:55:29 +0900 Subject: [PATCH] Display filename and line of user code when Rhymix\Framework\Exception is thrown --- classes/module/ModuleObject.class.php | 5 ++--- common/framework/Exception.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/classes/module/ModuleObject.class.php b/classes/module/ModuleObject.class.php index 0d5b4d3d8..e2ede1a5b 100644 --- a/classes/module/ModuleObject.class.php +++ b/classes/module/ModuleObject.class.php @@ -235,7 +235,7 @@ class ModuleObject extends BaseObject catch (Rhymix\Framework\Exception $e) { $this->stop($e->getMessage(), -2); - $this->add('rx_error_location', $e->getFile() . ':' . $e->getLine()); + $this->add('rx_error_location', $e->getUserFileAndLine()); } } @@ -857,8 +857,7 @@ class ModuleObject extends BaseObject catch (Rhymix\Framework\Exception $e) { $output = new BaseObject(-2, $e->getMessage()); - $location = $e->getFile() . ':' . $e->getLine(); - $output->add('rx_error_location', $location); + $output->add('rx_error_location', $e->getUserFileAndLine()); } // Trigger after specific action diff --git a/common/framework/Exception.php b/common/framework/Exception.php index 0ff93aa68..6cd07d412 100644 --- a/common/framework/Exception.php +++ b/common/framework/Exception.php @@ -7,5 +7,27 @@ namespace Rhymix\Framework; */ class Exception extends \Exception { + /** + * Get the file and line, skipping Rhymix framework files. + * + * This can be more helpful than just using getFile() and getLine() + * when the exception is thrown from a Rhymix framework file + * but the actual error is caused by a module or theme. + * + * @return string + */ + public function getUserFileAndLine(): string + { + $regexp = '!^' . preg_quote(\RX_BASEDIR, '!') . '(?:classes|common)/!'; + $trace = $this->getTrace(); + foreach ($trace as $frame) + { + if (!preg_match($regexp, $frame['file'])) + { + return $frame['file'] . ':' . $frame['line']; + } + } + return $this->getFile() . ':' . $this->getLine(); + } }