Skip to content

Commit

Permalink
Merge branch 'release/5.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Oct 2, 2024
2 parents 9c98981 + b9ccce6 commit 9f286b8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -21,6 +21,7 @@ return [
'anonymous' => true,
'clientDsn' => getenv('SENTRY_DSN') ?: 'https://[email protected]/123456789',
'excludedCodes' => ['400', '404', '429'],
'excludedExceptions' => [],
'release' => getenv('SENTRY_RELEASE') ?: null,
];
```
Expand All @@ -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)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 13 additions & 9 deletions src/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
'anonymous' => true,
'clientDsn' => getenv('SENTRY_DSN') ?: 'https://[email protected]/123456789',
'excludedCodes' => ['400', '404', '429'],
'excludedExceptions' => [],
'release' => getenv('SENTRY_RELEASE') ?: null,
];
5 changes: 5 additions & 0 deletions src/services/SentryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9f286b8

Please sign in to comment.