Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ return [
'anonymous' => false, // Determines to log user info or not
'clientDsn' => getenv('SENTRY_DSN') ?: 'https://example@sentry.io/123456789', // Set as string or use environment variable.
'clientKey' => getenv('SENTRY_CLIENT_KEY') ?: 'z987654321a', // https://js.sentry-cdn.com/z987654321a.min.js
'dataStorageLocation' => getenv('SENTRY_DATA_STORAGE_LOCATION') ?: 'US', // Can be either US or EU. Default is US if not specified
'excludedCodes' => ['400', '404', '429'],
'release' => getenv('SENTRY_RELEASE') ?: null, // Release number/name used by sentry.
'reportJsErrors' => false,
'sampleRate' => 1.0,
'allowUrls' => [], // URL string or regex. JS errors that have been created on these will be sent to Sentry.
'ignoreErrors' => [
// Email link Microsoft Outlook crawler compatibility error
// cf. https://forum.sentry.io/t/unhandledrejection-non-error-promise-rejection-captured-with-value/14062
Expand Down
32 changes: 27 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function init()
$info = $app->getInfo();
$settings = $this->getSettings();

if(!$settings->validate()) {
Craft::error('Sentry settings are invalid: ' . json_encode($settings->getErrors()), $this->handle);
throw new Exception('Invalid Sentry plugin settings: '. json_encode($settings->getErrors()));
return;
}

if (!$this->isInstalled || !$settings->enabled) return;

if (!$settings->clientDsn) {
Expand Down Expand Up @@ -125,23 +131,39 @@ public function init()
function (TemplateEvent $event) {
$settings = $this->getSettings();
$view = Craft::$app->getView();
$ignoreErrors = json_encode($settings->ignoreErrors);

$regexMap = [];

$allowUrls = array_map(function($url) use (&$regexMap) {
if (preg_match('/^\/(.+)\/([gimsuy]*)$/', $url, $m)) {
$key = '__REGEX_' . count($regexMap) . '__';
$regexMap['"' . $key . '"'] = '/' . $m[1] . '/' . $m[2];
return $key;
}
return $url;
}, $settings->allowUrls ?: []);

$allowUrls = json_encode($allowUrls, JSON_UNESCAPED_SLASHES);
$allowUrls = str_replace(array_keys($regexMap), array_values($regexMap), $allowUrls);

$dataStorageLocationSubdomain = $settings->dataStorageLocation == 'EU' ? 'js-de' : 'js';

$view->registerScript("
// Configure sentryOnLoad before adding the Loader Script
window.sentryOnLoad = function () {
Sentry.init({
release: '$settings->release',
environment: '" . App::env('CRAFT_ENVIRONMENT') . "',
ignoreErrors: $ignoreErrors,
release: '$settings->release',
environment: '" . App::env('CRAFT_ENVIRONMENT') . "',
ignoreErrors: " . json_encode($settings->ignoreErrors) . ",
allowUrls: $allowUrls
});
};", View::POS_END, $this->getScriptOptions());

$view->registerScript(
"",
View::POS_END,
array_merge([
'src' => "https://js.sentry-cdn.com/$settings->clientKey.min.js",
'src' => "https://".$dataStorageLocationSubdomain.".sentry-cdn.com" ."/$settings->clientKey.min.js",
'crossorigin' => 'anonymous',
], $this->getScriptOptions())
);
Expand Down
13 changes: 8 additions & 5 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class Settings extends Model
{
public $enabled = true;
public $anonymous = false; // Determines to log user info or not
public $clientDsn;
public $clientKey;
public string $clientDsn;
public string $clientKey;
public $dataStorageLocation = 'US';
public $excludedCodes = ['404'];
public $release; // Release number/name used by sentry.
public string $release; // Release number/name used by sentry.
public $reportJsErrors = false; // Client only option
public $sampleRate = 1.0; // Client only option
public $ignoreErrors = [];
public $allowUrls = [];

/**
* @inheritdoc
Expand All @@ -23,10 +25,11 @@ public function defineRules(): array
{
return [
[['enabled', 'anonymous', 'reportJsErrors'], 'boolean'],
[['clientDsn', 'clientKey', 'excludedCodes', 'release'], 'string'],
[['ignoreErrors'], 'array'],
[['clientDsn', 'clientKey', 'release', 'dataStorageLocation'], 'string'],
[['excludedCodes', 'ignoreErrors', 'allowUrls'], 'each', 'rule' => ['string']],
[['clientDsn'], 'required'],
[['sampleRate'], 'number', 'min' => 0, 'max' => 1],
[['dataStorageLocation'], 'in', 'range' => ['US', 'EU'], 'message' => 'Data Storage Location must be either "US" or "EU".'],
];
}
}