diff --git a/README.md b/README.md index 96c5136..0858236 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Sentry.io for Craft CMS 3.x +# Sentry.io for Craft CMS [Sentry.io](https://sentry.io/) integration for Craft CMS. Inspired by [born05/craft-sentry](https://github.com/born05/craft-sentry), but with our own twist. @@ -21,6 +21,7 @@ return [ 'anonymous' => true, 'clientDsn' => getenv('SENTRY_DSN') ?: 'https://example@sentry.io/123456789', 'excludedCodes' => ['400', '404', '429'], + 'excludedExceptions' => [], 'release' => getenv('SENTRY_RELEASE') ?: null, ]; ``` @@ -45,6 +46,14 @@ try { The plugin works for exceptions thrown in web requests as well as console requests. For web requests, the url where the error happened is included. +### Excluding specific exceptions +Using the ``excludedExceptions``, you can stop specific types of exceptions from being logged to Sentry, for example: +````php +'excludedExceptions' => [ + \craft\errors\ImageTransformException::class, +], +```` + --- Brought to you by [Statik.be](https://www.statik.be) diff --git a/composer.json b/composer.json index fbca454..9a945de 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "statikbe/craft-sentry", "description": "Sentry.io integration for Craft CMS", "type": "craft-plugin", - "version": "5.0.0", + "version": "5.1.0", "keywords": [ "craft", "sentry", diff --git a/src/Sentry.php b/src/Sentry.php index e5fc79d..c15442c 100644 --- a/src/Sentry.php +++ b/src/Sentry.php @@ -54,22 +54,26 @@ public function init(): void 'sentry' => SentryService::class, ]; - Event::on( - ErrorHandler::className(), - ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, - function(ExceptionEvent $event) { - if ($this->getSettings()->enabled) { - $this->sentry->handleException($event->exception); + Craft::$app->onInit(function () { + Event::on( + ErrorHandler::className(), + ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, + function (ExceptionEvent $event) { + if ($this->getSettings()->enabled) { + $this->sentry->handleException($event->exception); + } } - } - ); + ); + }); } // Static Methods // ========================================================================= public static function handleException($exception) { - Sentry::$plugin->sentry->handleException($exception); + if (self::$plugin->getSettings()->enabled) { + self::$plugin->sentry->handleException($exception); + } } // Protected Methods diff --git a/src/config.php b/src/config.php index 3e698b3..cdf1860 100644 --- a/src/config.php +++ b/src/config.php @@ -27,5 +27,6 @@ 'anonymous' => true, 'clientDsn' => getenv('SENTRY_DSN') ?: 'https://example@sentry.io/123456789', 'excludedCodes' => ['400', '404', '429'], + 'excludedExceptions' => [], 'release' => getenv('SENTRY_RELEASE') ?: null, ]; diff --git a/src/services/SentryService.php b/src/services/SentryService.php index 978f67b..8695269 100644 --- a/src/services/SentryService.php +++ b/src/services/SentryService.php @@ -36,6 +36,11 @@ public function handleException($exception) return; } + if(in_array(get_class($exception), $settings->excludedExceptions)) { + Craft::info('Exception class excluded from being reported to Sentry.', $plugin->handle); + return; + } + $this->setupSentry(); Craft::info('Send exception to Sentry.', $plugin->handle);