Skip to content

Commit

Permalink
Populate all captures with additional data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Dec 12, 2019
1 parent 60b78c6 commit c3a78f8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 83 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,7 +24,7 @@
],
"require": {
"craftcms/cms": "^3.1",
"sentry/sdk": "^2.0.4"
"sentry/sdk": "^2.0.5"
},
"autoload": {
"psr-4": {
Expand Down
45 changes: 37 additions & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
});
}

/**
Expand Down
73 changes: 0 additions & 73 deletions src/services/SentryService.php

This file was deleted.

0 comments on commit c3a78f8

Please sign in to comment.