diff --git a/CHANGELOG.md b/CHANGELOG.md index b6aeea9..d775d20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Craft Sentry Changelog +## 1.0.1 - 2019-12-12 +### Change +- Populate all captures with additional data. +- Don't use craft handled error events. + ## 1.0.0 - 2019-11-26 ### Added - Stable release diff --git a/composer.json b/composer.json index f834618..70a4a22 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "born05/craft-sentry", "description": "Pushes Craft CMS errors to Sentry.", "type": "craft-plugin", - "version": "1.0.0", + "version": "1.0.1", "keywords": [ "craft", "cms", @@ -24,7 +24,7 @@ ], "require": { "craftcms/cms": "^3.1", - "sentry/sdk": "^2.0.4" + "sentry/sdk": "^2.0.5" }, "autoload": { "psr-4": { diff --git a/src/Plugin.php b/src/Plugin.php index a2f5879..39ab635 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -9,6 +9,9 @@ use craft\events\ExceptionEvent; use craft\web\ErrorHandler; +use Sentry; +use Sentry\State\Scope; + use yii\base\Event; class Plugin extends CraftPlugin @@ -29,17 +32,43 @@ public function init() parent::init(); self::$plugin = $this; - $this->setComponents([ - 'sentry' => SentryService::class, + $app = Craft::$app; + $info = $app->getInfo(); + $settings = $this->getSettings(); + + if (!$settings->enabled || $app->getConfig()->getGeneral()->devMode) return; + + if (!$settings->clientDsn) { + Craft::error('Failed to report exception due to missing client key (DSN)', $this->handle); + return; + } + + Sentry\init([ + 'dsn' => $settings->clientDsn, + 'environment' => CRAFT_ENVIRONMENT, + 'release' => $settings->release, ]); - Event::on( - ErrorHandler::className(), - ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, - function(ExceptionEvent $event) { - $this->sentry->handleException($event->exception); + $user = $app->getUser()->getIdentity(); + + Sentry\configureScope(function (Scope $scope) use ($app, $info, $settings, $user) { + if ($user && !$settings->anonymous) { + $scope->setUser([ + 'ID' => $user->id, + 'Username' => $user->username, + 'Email' => $user->email, + 'Admin' => $user->admin ? 'Yes' : 'No', + ]); } - ); + + $scope->setExtra('App Type', 'Craft CMS'); + $scope->setExtra('App Name', $info->name); + $scope->setExtra('App Edition (licensed)', $app->getLicensedEditionName()); + $scope->setExtra('App Edition (running)', $app->getEditionName()); + $scope->setExtra('App Version', $info->version); + $scope->setExtra('App Version (schema)', $info->schemaVersion); + $scope->setExtra('PHP Version', phpversion()); + }); } /** diff --git a/src/services/SentryService.php b/src/services/SentryService.php deleted file mode 100644 index 2cd3425..0000000 --- a/src/services/SentryService.php +++ /dev/null @@ -1,73 +0,0 @@ -getInfo(); - $plugin = SentryPlugin::$plugin; - $settings = $plugin->getSettings(); - - if (!$settings->enabled || $app->getConfig()->getGeneral()->devMode) return; - - if (!$settings->clientDsn) { - Craft::error('Failed to report exception due to missing client key (DSN)', $plugin->handle); - return; - } - - // If this is a Twig Runtime exception, use the previous one instead - if ($exception instanceof \Twig_Error_Runtime && ($previousException = $exception->getPrevious()) !== null) { - $exception = $previousException; - } - - $statusCode = $exception->statusCode ?? null; - - if (in_array($statusCode, $settings->excludedCodes)) { - Craft::info('Exception status code excluded from being reported to Sentry.', $plugin->handle); - return; - } - - Sentry\init([ - 'dsn' => $settings->clientDsn, - 'environment' => CRAFT_ENVIRONMENT, - 'release' => $settings->release, - ]); - - $user = $app->getUser()->getIdentity(); - - Sentry\configureScope(function (Scope $scope) use ($app, $info, $plugin, $settings, $user, $statusCode) { - if ($user && !$settings->anonymous) { - $scope->setUser([ - 'ID' => $user->id, - 'Username' => $user->username, - 'Email' => $user->email, - 'Admin' => $user->admin ? 'Yes' : 'No', - ]); - } - - $scope->setExtra('App Type', 'Craft CMS'); - $scope->setExtra('App Name', $info->name); - $scope->setExtra('App Edition (licensed)', $app->getLicensedEditionName()); - $scope->setExtra('App Edition (running)', $app->getEditionName()); - $scope->setExtra('App Version', $info->version); - $scope->setExtra('App Version (schema)', $info->schemaVersion); - $scope->setExtra('PHP Version', phpversion()); - $scope->setExtra('Status Code', $statusCode); - }); - - Sentry\captureException($exception); - } -}