forked from colin/simple-glitchtip
Update simple-glitchtip.php
This commit is contained in:
parent
deb9f43a41
commit
12e14401d2
|
@ -1,72 +1,76 @@
|
|||
<?php
|
||||
/*
|
||||
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
|
||||
*/
|
||||
namespace Concrete\Package\SimpleSentry;
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
use Concrete\Core\Package\Package;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Sentry\ClientBuilder;
|
||||
use Sentry\State\HubInterface;
|
||||
|
||||
// Register settings
|
||||
function sg_register_settings() {
|
||||
register_setting('sg-settings-group', 'sg_dsn');
|
||||
}
|
||||
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.';
|
||||
|
||||
// Settings page
|
||||
function sg_settings_page() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Simple Glitchtip Settings</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php settings_fields('sg-settings-group'); ?>
|
||||
<?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
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// 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');
|
||||
|
|
Loading…
Reference in New Issue