forked from colin/simple-glitchtip
Update simple-glitchtip.php
This commit is contained in:
parent
deb9f43a41
commit
12e14401d2
|
@ -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;
|
||||||
|
|
||||||
// Register settings
|
class Controller extends Package
|
||||||
function sg_register_settings() {
|
{
|
||||||
register_setting('sg-settings-group', 'sg_dsn');
|
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.';
|
||||||
|
|
||||||
// Settings page
|
public function on_start()
|
||||||
function sg_settings_page() {
|
{
|
||||||
?>
|
$this->app->singleton(HubInterface::class, function () {
|
||||||
<div class="wrap">
|
$dsn = getenv('GLITCHTIP_DSN'); // Pull the DSN from the environment variable
|
||||||
<h1>Simple Glitchtip Settings</h1>
|
if (!$dsn) {
|
||||||
<form method="post" action="options.php">
|
\Log::warning('SimpleSentry: GLITCHTIP_DSN environment variable is not set.');
|
||||||
<?php settings_fields('sg-settings-group'); ?>
|
return null;
|
||||||
<?php do_settings_sections('sg-settings-group'); ?>
|
}
|
||||||
<table class="form-table">
|
|
||||||
<tr valign="top">
|
|
||||||
<th scope="row">DSN</th>
|
|
||||||
<td><input type="text" name="sg_dsn" value="<?php echo esc_attr(get_option('sg_dsn')); ?>" /></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
<?php submit_button(); ?>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
<?php
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add settings menu
|
$clientBuilder = ClientBuilder::create([
|
||||||
function sg_add_settings_menu() {
|
'dsn' => $dsn,
|
||||||
add_options_page('Simple Glitchtip Settings', 'Glitchtip', 'manage_options', 'sg-settings', 'sg_settings_page');
|
'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'
|
||||||
add_action('admin_menu', 'sg_add_settings_menu');
|
]);
|
||||||
add_action('admin_init', 'sg_register_settings');
|
|
||||||
|
|
||||||
// Initialize error handler
|
return $clientBuilder->getHub();
|
||||||
function sg_initialize_error_handler($dsn) {
|
});
|
||||||
if (!class_exists('Sentry\State\Hub')) {
|
|
||||||
require_once plugin_dir_path(__FILE__) . 'sentry.phar';
|
// 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) {
|
// Log the Sentry event ID in Concrete5
|
||||||
Sentry\captureException($exception);
|
\Log::error('SimpleSentry: Sentry Event ID: ' . $eventId);
|
||||||
}
|
}
|
||||||
set_exception_handler('handle_exception');
|
});
|
||||||
|
|
||||||
function handle_error($errno, $errstr, $errfile, $errline) {
|
// Catch PHP errors
|
||||||
Sentry\captureMessage("$errstr in $errfile on line $errline");
|
set_error_handler(function ($severity, $message, $file, $line) {
|
||||||
}
|
if (!(error_reporting() & $severity)) {
|
||||||
set_error_handler('handle_error');
|
// Error not included in error_reporting
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the plugin
|
$hub = $this->app->make(HubInterface::class);
|
||||||
function sg_initialize_plugin() {
|
if ($hub) {
|
||||||
$dsn = getenv('SENTRY_DSN') ?: getenv('GLITCHTIP_DSN') ?: get_option('sg_dsn');
|
$exception = new \ErrorException($message, 0, $severity, $file, $line);
|
||||||
if ($dsn) {
|
$eventId = $hub->captureException($exception);
|
||||||
sg_initialize_error_handler($dsn);
|
|
||||||
|
// Log the Sentry event ID in Concrete5
|
||||||
|
\Log::error('SimpleSentry: Sentry Event ID: ' . $eventId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
add_action('plugins_loaded', 'sg_initialize_plugin');
|
|
||||||
|
|
Loading…
Reference in New Issue