From 12e14401d2f746cccb77ccbf536c536d7003f7b8 Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 14 Jan 2025 11:52:58 -0500 Subject: [PATCH] Update simple-glitchtip.php --- simple-glitchtip.php | 122 ++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/simple-glitchtip.php b/simple-glitchtip.php index 2200165..601b3bf 100644 --- a/simple-glitchtip.php +++ b/simple-glitchtip.php @@ -1,72 +1,76 @@ -
-

Simple Glitchtip Settings

-
- - - - - - - -
DSN
- -
-
- app->singleton(HubInterface::class, function () { + $dsn = getenv('GLITCHTIP_DSN'); // Pull the DSN from the environment variable + if (!$dsn) { + \Log::warning('SimpleSentry: GLITCHTIP_DSN environment variable is not set.'); + return null; + } -// Add settings menu -function sg_add_settings_menu() { - add_options_page('Simple Glitchtip Settings', 'Glitchtip', 'manage_options', 'sg-settings', 'sg_settings_page'); -} -add_action('admin_menu', 'sg_add_settings_menu'); -add_action('admin_init', 'sg_register_settings'); + $clientBuilder = ClientBuilder::create([ + 'dsn' => $dsn, + 'environment' => getenv('APP_ENV') ?: 'production', // Use APP_ENV or default to 'production' + 'release' => getenv('APP_VERSION') ?: '1.0.0', // Use APP_VERSION or default to '1.0.0' + ]); -// Initialize error handler -function sg_initialize_error_handler($dsn) { - if (!class_exists('Sentry\State\Hub')) { - require_once plugin_dir_path(__FILE__) . 'sentry.phar'; + return $clientBuilder->getHub(); + }); + + // Attach error handlers only if the HubInterface is properly initialized + $hub = $this->app->make(HubInterface::class); + if ($hub) { + $this->attachErrorHandlers(); + } else { + \Log::warning('SimpleSentry: HubInterface is not initialized. Error reporting is disabled.'); + } } - Sentry\init(['dsn' => $dsn]); + private function attachErrorHandlers() + { + // Catch uncaught exceptions + set_exception_handler(function (\Throwable $exception) { + $hub = $this->app->make(HubInterface::class); + if ($hub) { + $eventId = $hub->captureException($exception); - function handle_exception($exception) { - Sentry\captureException($exception); - } - set_exception_handler('handle_exception'); + // Log the Sentry event ID in Concrete5 + \Log::error('SimpleSentry: Sentry Event ID: ' . $eventId); + } + }); - function handle_error($errno, $errstr, $errfile, $errline) { - Sentry\captureMessage("$errstr in $errfile on line $errline"); - } - set_error_handler('handle_error'); -} + // Catch PHP errors + set_error_handler(function ($severity, $message, $file, $line) { + if (!(error_reporting() & $severity)) { + // Error not included in error_reporting + return false; + } -// Initialize the plugin -function sg_initialize_plugin() { - $dsn = getenv('SENTRY_DSN') ?: getenv('GLITCHTIP_DSN') ?: get_option('sg_dsn'); - if ($dsn) { - sg_initialize_error_handler($dsn); + $hub = $this->app->make(HubInterface::class); + if ($hub) { + $exception = new \ErrorException($message, 0, $severity, $file, $line); + $eventId = $hub->captureException($exception); + + // Log the Sentry event ID in Concrete5 + \Log::error('SimpleSentry: Sentry Event ID: ' . $eventId); + } + + return true; + }); } } -add_action('plugins_loaded', 'sg_initialize_plugin');