forked from colin/simple-glitchtip
Update simple-glitchtip.php
This commit is contained in:
parent
f705ec6810
commit
8e97007a7e
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
namespace Concrete\Package\SimpleSentry;
|
||||
|
||||
namespace Concrete\Package\SimpleGlitchtip;
|
||||
|
||||
use Concrete\Core\Package\Package;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
@ -8,56 +9,53 @@ use Sentry\State\HubInterface;
|
|||
|
||||
class Controller extends Package
|
||||
{
|
||||
protected $pkgHandle = 'simple_sentry';
|
||||
protected $pkgHandle = 'simple_glitchtip';
|
||||
protected $appVersionRequired = '9.0.0';
|
||||
protected $pkgVersion = '1.0.0';
|
||||
protected $pkgName = 'SimpleSentry Error Reporter';
|
||||
protected $pkgName = 'SimpleGlitchtip 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
|
||||
$dsn = getenv('GLITCHTIP_DSN');
|
||||
if (!$dsn) {
|
||||
\Log::warning('SimpleSentry: GLITCHTIP_DSN environment variable is not set.');
|
||||
\Log::warning('SimpleGlitchtip: GLITCHTIP_DSN environment variable is not set.');
|
||||
return null;
|
||||
}
|
||||
|
||||
$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'
|
||||
'environment' => getenv('APP_ENV') ?: 'production',
|
||||
'release' => getenv('APP_VERSION') ?: '1.0.0',
|
||||
'tags' => [
|
||||
'application' => 'concrete5',
|
||||
],
|
||||
]);
|
||||
|
||||
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.');
|
||||
\Log::warning('SimpleGlitchtip: HubInterface is not initialized. Error reporting is disabled.');
|
||||
}
|
||||
}
|
||||
|
||||
private function attachErrorHandlers()
|
||||
{
|
||||
// Catch uncaught exceptions
|
||||
set_exception_handler(function (\Throwable $exception) {
|
||||
$hub = $this->app->make(HubInterface::class);
|
||||
if ($hub) {
|
||||
$eventId = $hub->captureException($exception);
|
||||
|
||||
// Log the Sentry event ID in Concrete5
|
||||
\Log::error('SimpleSentry: Sentry Event ID: ' . $eventId);
|
||||
\Log::error('SimpleGlitchtip: Sentry Event ID: ' . $eventId);
|
||||
}
|
||||
});
|
||||
|
||||
// Catch PHP errors
|
||||
set_error_handler(function ($severity, $message, $file, $line) {
|
||||
if (!(error_reporting() & $severity)) {
|
||||
// Error not included in error_reporting
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -65,12 +63,22 @@ class Controller extends Package
|
|||
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);
|
||||
\Log::error('SimpleGlitchtip: Sentry Event ID: ' . $eventId);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
register_shutdown_function(function () {
|
||||
$error = error_get_last();
|
||||
if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
|
||||
$hub = $this->app->make(HubInterface::class);
|
||||
if ($hub) {
|
||||
$exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
|
||||
$eventId = $hub->captureException($exception);
|
||||
\Log::error('SimpleGlitchtip: Sentry Event ID (shutdown error): ' . $eventId);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue