Skip to content

Commit af50a73

Browse files
committed
[FEATURE] prevent error reporting after user has navigated during pageload
1 parent 58faadc commit af50a73

File tree

4 files changed

+73
-40
lines changed

4 files changed

+73
-40
lines changed

Block/SentryScript.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,9 @@ public function getIgnoreJsErrors(): string
170170
{
171171
return $this->json->serialize($this->dataHelper->getIgnoreJsErrors());
172172
}
173+
174+
public function isPreventReportingAfterUnload(): bool
175+
{
176+
return $this->dataHelper->isPreventReportingAfterUnload();
177+
}
173178
}

Helper/Data.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Data extends AbstractHelper
3636
'js_sdk_version',
3737
'tracing_enabled',
3838
'tracing_sample_rate',
39-
'ignore_js_errors'
39+
'ignore_js_errors',
40+
'prevent_reporting_after_unload'
4041
];
4142

4243
/**
@@ -344,4 +345,9 @@ public function shouldCaptureException(\Throwable $ex)
344345

345346
return true;
346347
}
348+
349+
public function isPreventReportingAfterUnload(): bool
350+
{
351+
return (bool) ($this->config['prevent_reporting_after_unload'] ?? false);
352+
}
347353
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ This module uses the [Magento Deployment Configuration](https://devdocs.magento.
2424
'js_sdk_version' => \JustBetter\Sentry\Block\SentryScript::CURRENT_VERSION
2525
'tracing_enabled' => true,
2626
'tracing_sample_rate' => 0.5,
27-
'ignore_js_errors' => []
27+
'ignore_js_errors' => [],
28+
'prevent_reporting_after_unload' => false
2829
]
2930
```
3031

@@ -41,6 +42,7 @@ Next to that there are some configuration options under Stores > Configuration >
4142
* `tracing_enabled` if this option is set to true, tracing got enabled (bundle file got loaded automatically). Default: `false`
4243
* `tracing_sample_rate` if tracing is enabled, you should also set the sample rate. Default: `0.2`
4344
* `ignore_js_errors` array of javascript error messages, which should be not send to Sentry.
45+
* `prevent_reporting_after_unload` if this option is set to `true`, no errors will be reported after unload event. Can be useful, when the user navigates to an different page, during loading of resources by requirejs.
4446

4547
## Optional error page configuration
4648
- Optional you can configure custom error pages in pub/errors. You can use the sentry feedback form and insert here the sentry log ID. The Sentry Log Id is captured in de customer session and can be retrieved in `processor.php`.

view/frontend/templates/script/sentry.phtml

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,68 @@ $remoteFile = sprintf('https://browser.sentry-cdn.com/%s/%s', $block->escapeHtml
1717
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Promise" crossorigin></script>
1818
<script src="<?= /** @noEscape */$remoteFile ?>" crossorigin="anonymous"></script>
1919
<script>
20-
Sentry.init({
21-
dsn: '<?= $block->escapeUrl(trim($block->getDSN())) ?>',
22-
release: '<?= $block->escapeHtml(trim($block->getVersion())) ?>',
23-
environment: '<?= $block->escapeHtml(trim($block->getEnvironment())) ?>',
24-
integrations: [
25-
<?php if ($block->isTracingEnabled()) : ?>
26-
new Sentry.BrowserTracing(),
27-
<?php endif ?>
28-
],
29-
<?php if ($block->isTracingEnabled()) : ?>
30-
tracesSampleRate: <?=$block->getTracingSampleRate()?>,
31-
<?php endif ?>
32-
ignoreErrors: <?= /** @noEscape */ $block->getIgnoreJsErrors() ?>,
33-
<?php if ($block->stripStaticContentVersion() || $block->stripStoreCode()): ?>
34-
beforeSend: function(event) {
35-
event.exception.values.map(function (value) {
36-
if (value.stacktrace === undefined || ! value.stacktrace) {
37-
return value;
38-
}
20+
(function () {
21+
<?php if ($block->isPreventReportingAfterUnload()): ?>
22+
let preventSentryReporting = false;
23+
window.addEventListener('beforeunload', () => {
24+
preventSentryReporting = true;
25+
});
3926

40-
<?php if ($block->stripStaticContentVersion()): ?>
41-
value.stacktrace.frames.map(function (frame) {
42-
frame.filename = frame.filename.replace(/version[0-9]{10}\//, '');
43-
return frame;
44-
});
45-
<?php endif; ?>
27+
if (preventSentryReporting) {
28+
return;
29+
}
30+
<?php endif ?>
4631

47-
<?php if ($block->stripStoreCode()): ?>
48-
value.stacktrace.frames.map(function (frame) {
49-
<?php // phpcs:disable Generic.Files.LineLength ?>
50-
frame.filename = frame.filename.replace('/<?= $block->escapeHtml($block->getStoreCode()); ?>/', '/');
51-
<?php // phpcs:enable Generic.Files.LineLength ?>
52-
return frame;
53-
});
32+
Sentry.init({
33+
dsn: '<?= $block->escapeUrl(trim($block->getDSN())) ?>',
34+
release: '<?= $block->escapeHtml(trim($block->getVersion())) ?>',
35+
environment: '<?= $block->escapeHtml(trim($block->getEnvironment())) ?>',
36+
integrations: [
37+
<?php if ($block->isTracingEnabled()): ?>
38+
new Sentry.BrowserTracing(),
39+
<?php endif ?>
40+
],
41+
<?php if ($block->isTracingEnabled()): ?>
42+
tracesSampleRate: <?=$block->getTracingSampleRate()?>,
43+
<?php endif ?>
44+
ignoreErrors: <?= /** @noEscape */ $block->getIgnoreJsErrors() ?>,
45+
beforeSend: function (event) {
46+
<?php if ($block->isPreventReportingAfterUnload()): ?>
47+
if (preventSentryReporting) {
48+
return null;
49+
}
50+
<?php endif ?>
51+
52+
<?php if ($block->stripStaticContentVersion() || $block->stripStoreCode()): ?>
53+
event.exception.values.map(function (value) {
54+
if (value.stacktrace === undefined || !value.stacktrace) {
55+
return value;
56+
}
57+
58+
<?php if ($block->stripStaticContentVersion()): ?>
59+
value.stacktrace.frames.map(function (frame) {
60+
frame.filename = frame.filename.replace(/version[0-9]{10}\//, '');
61+
return frame;
62+
});
63+
<?php endif; ?>
64+
65+
<?php if ($block->stripStoreCode()): ?>
66+
value.stacktrace.frames.map(function (frame) {
67+
<?php // phpcs:disable Generic.Files.LineLength ?>
68+
frame.filename = frame.filename.replace('/<?= $block->escapeHtml($block->getStoreCode()); ?>/', '/');
69+
<?php // phpcs:enable Generic.Files.LineLength ?>
70+
return frame;
71+
});
72+
<?php endif; ?>
73+
74+
return value;
75+
});
5476
<?php endif; ?>
5577

56-
return value;
57-
});
58-
return event;
59-
}
60-
<?php endif; ?>
61-
});
78+
return event;
79+
}
80+
});
81+
})();
6282
</script>
6383

6484
<?php if ($block->useLogRocket()): ?>

0 commit comments

Comments
 (0)