Update simple-glitchtip.php

This commit is contained in:
colin 2025-01-14 11:52:58 -05:00
parent deb9f43a41
commit 12e14401d2
1 changed files with 63 additions and 59 deletions

View File

@ -1,72 +1,76 @@
<?php <?php
/* namespace Concrete\Package\SimpleSentry;
Plugin Name: Simple Glitchtip
Description: A simple plugin to handle Sentry/Glitchtip DSNs and report errors.
Version: 1.0
Author: Colin
Plugin URI: https://git.nixc.us/colin/simple-glitchtip
*/
if (!defined('ABSPATH')) { use Concrete\Core\Package\Package;
exit; // Exit if accessed directly. use Symfony\Component\HttpFoundation\Request;
use Sentry\ClientBuilder;
use Sentry\State\HubInterface;
class Controller extends Package
{
protected $pkgHandle = 'simple_sentry';
protected $appVersionRequired = '9.0.0';
protected $pkgVersion = '1.0.0';
protected $pkgName = 'SimpleSentry Error Reporter';
protected $pkgDescription = 'Reports errors to Sentry/GlitchTip using the GLITCHTIP_DSN environment variable.';
public function on_start()
{
$this->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;
} }
// Register settings $clientBuilder = ClientBuilder::create([
function sg_register_settings() { 'dsn' => $dsn,
register_setting('sg-settings-group', 'sg_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'
]);
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.');
}
} }
// Settings page private function attachErrorHandlers()
function sg_settings_page() { {
?> // Catch uncaught exceptions
<div class="wrap"> set_exception_handler(function (\Throwable $exception) {
<h1>Simple Glitchtip Settings</h1> $hub = $this->app->make(HubInterface::class);
<form method="post" action="options.php"> if ($hub) {
<?php settings_fields('sg-settings-group'); ?> $eventId = $hub->captureException($exception);
<?php do_settings_sections('sg-settings-group'); ?>
<table class="form-table"> // Log the Sentry event ID in Concrete5
<tr valign="top"> \Log::error('SimpleSentry: Sentry Event ID: ' . $eventId);
<th scope="row">DSN</th> }
<td><input type="text" name="sg_dsn" value="<?php echo esc_attr(get_option('sg_dsn')); ?>" /></td> });
</tr>
</table> // Catch PHP errors
<?php submit_button(); ?> set_error_handler(function ($severity, $message, $file, $line) {
</form> if (!(error_reporting() & $severity)) {
</div> // Error not included in error_reporting
<?php return false;
} }
// Add settings menu $hub = $this->app->make(HubInterface::class);
function sg_add_settings_menu() { if ($hub) {
add_options_page('Simple Glitchtip Settings', 'Glitchtip', 'manage_options', 'sg-settings', 'sg_settings_page'); $exception = new \ErrorException($message, 0, $severity, $file, $line);
} $eventId = $hub->captureException($exception);
add_action('admin_menu', 'sg_add_settings_menu');
add_action('admin_init', 'sg_register_settings');
// Initialize error handler // Log the Sentry event ID in Concrete5
function sg_initialize_error_handler($dsn) { \Log::error('SimpleSentry: Sentry Event ID: ' . $eventId);
if (!class_exists('Sentry\State\Hub')) {
require_once plugin_dir_path(__FILE__) . 'sentry.phar';
} }
Sentry\init(['dsn' => $dsn]); return true;
});
function handle_exception($exception) {
Sentry\captureException($exception);
}
set_exception_handler('handle_exception');
function handle_error($errno, $errstr, $errfile, $errline) {
Sentry\captureMessage("$errstr in $errfile on line $errline");
}
set_error_handler('handle_error');
}
// 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);
} }
} }
add_action('plugins_loaded', 'sg_initialize_plugin');