Skip to content

Commit 63cc7af

Browse files
aschemppfritzmgbytehead
authoredMay 23, 2024
Include default configuration for Contao managed edition (#12)
Co-authored-by: Fritz Michael Gschwantner <[email protected]> Co-authored-by: David Greminger <[email protected]>
1 parent 0ba8321 commit 63cc7af

11 files changed

+133
-70
lines changed
 

‎README.md

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
Contao Sentry Bundle
22
====================
33

4-
This Contao bundle provides an easy integration of [sentry.io](https://sentry.io/) for Contao 4.4.x and newer.
4+
This Contao bundle provides an easy integration of [sentry.io](https://sentry.io/) for Contao 4.13 and 5.x.
55

66
[![Author](http://img.shields.io/badge/author-@1upgmbh-blue.svg?style=flat-square)](https://twitter.com/1upgmbh)
77
[![Software License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
88
[![Total Downloads](http://img.shields.io/packagist/dt/oneup/contao-sentry-bundle.svg?style=flat-square)](https://packagist.org/packages/oneup/contao-sentry-bundle)
99

1010
--
1111

12-
In the first place, this is an "wrapper extension" for the [`sentry/sentry-symfony` bundle][1]. Therefore, you need to
13-
configure this bundle as you would configure the `sentry/sentry-symfony` bundle: [Documentation][2]
14-
15-
### Recommended configuration
16-
17-
If you also want to report the system log errors to Sentry, this is the recommended configuration:
18-
19-
```yml
20-
sentry:
21-
dsn: "https://xyz@sentry.io/xy"
22-
register_error_listener: false
23-
24-
Sentry\Monolog\Handler:
25-
arguments:
26-
$hub: '@Sentry\State\HubInterface'
27-
$level: !php/const Monolog\Logger::ERROR # Can be one of https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels, but System::log() only uses INFO or ERROR
28-
$bubble: false
29-
30-
monolog:
31-
handlers:
32-
sentry:
33-
type: service
34-
id: Sentry\Monolog\Handler
35-
priority: 100 # Higher priority than ContaoTableHandler which will stop handling afterwards (bubbling is set to true)
36-
bubble: false # Use bubble: true if you don't want the logs to show up in the system log (bubbling means, no monolog handlers will run afterwards)
37-
```
12+
This is a "wrapper extension" for the [`sentry/sentry-symfony` bundle][1].
13+
14+
15+
### Setup in the Contao Managed Edition
16+
17+
The basic integration is automatically configured to some sane defaults. To enable
18+
the integration, configure the `SENTRY_DSN` variable in your `.env.local` file.
19+
20+
Additionally, you can name the `SENTRY_ENV` in your `.env.local` file, which can be useful
21+
if you e.g. have a `test` and `prod` installation.
22+
23+
If you need to change any of the defaults, simply configure
24+
the `sentry/sentry-symfony` bundle according to the [Documentation][2].
25+
26+
27+
### Manual configuration
28+
29+
If you do not use the Contao Managed Edition, you need to configure this bundle as you would
30+
configure the `sentry/sentry-symfony` bundle: [Documentation][2]
31+
3832

3933
### User feedback
4034

@@ -44,7 +38,7 @@ some comments.
4438

4539
In order to integrate this feature, you have to alter the error page template. Place a copy of
4640
`vendor/contao/core-bundle/src/Resources/views/Error/layout.html.twig` in the directory
47-
`app/Resources/ContaoCoreBundle/views/Error/`.
41+
`templates/ContaoCoreBundle/views/Error/`.
4842

4943
Modify the copied template and place the following snippet just before the closing `</body>` tag:
5044
```twig
@@ -70,6 +64,6 @@ Modify the copied template and place the following snippet just before the closi
7064

7165

7266
[1]: https://github.com/getsentry/sentry-symfony/
73-
[2]: https://github.com/getsentry/sentry-symfony/#configuration-of-the-sdk
67+
[2]: https://docs.sentry.io/platforms/php/guides/symfony/#install
7468
[3]: https://docs.sentry.io/learn/user-feedback/
7569
[4]: https://user-images.githubusercontent.com/1284725/41782120-a06637f0-7639-11e8-96d7-a053e7ddd232.png

‎composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^7.1 || ^8.0",
24-
"contao/core-bundle": "^4.4 || ^5.0",
25-
"sentry/sentry-symfony": "^4.0 || ^5.0",
23+
"php": "^8.1",
24+
"contao/core-bundle": "^4.13 || ^5.0",
25+
"sentry/sentry-symfony": "^5.0",
2626
"twig/twig": "^2.7 || ^3.0"
2727
},
2828
"require-dev": {
@@ -43,11 +43,11 @@
4343
}
4444
},
4545
"extra": {
46-
"contao-manager-plugin": "Oneup\\Contao\\SentryBundle\\ContaoManager\\Plugin"
46+
"contao-manager-plugin": "Oneup\\ContaoSentryBundle\\ContaoManager\\Plugin"
4747
},
4848
"autoload": {
4949
"psr-4": {
50-
"Oneup\\Contao\\SentryBundle\\": "src"
50+
"Oneup\\ContaoSentryBundle\\": "src"
5151
}
5252
},
5353
"support": {

‎config/services.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
oneup_contao_sentry.twig.extension:
3+
class: Oneup\ContaoSentryBundle\Twig\ContaoSentryTwigExtension
4+
tags:
5+
- { name: twig.extension }
6+
7+
oneup_contao_sentry.twig.runtime:
8+
class: Oneup\ContaoSentryBundle\Twig\ContaoSentryTwigRuntime
9+
public: false
10+
tags:
11+
- { name: twig.runtime }
12+
13+
oneup_contao_sentry.integration.ignore_previous_exceptions:
14+
class: Oneup\ContaoSentryBundle\Integration\IgnorePreviousExceptionsIntegration

‎config/skeleton.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
parameters:
2+
env(SENTRY_DSN): ''
3+
env(SENTRY_ENV): '%kernel.environment%'
4+
5+
sentry:
6+
dsn: '%env(SENTRY_DSN)%'
7+
register_error_listener: false
8+
register_error_handler: false
9+
options:
10+
environment: "%env(SENTRY_ENV)%"
11+
before_send: oneup_contao_sentry.integration.ignore_previous_exceptions
12+
ignore_exceptions:
13+
- Contao\CoreBundle\Exception\AccessDeniedException
14+
- Contao\CoreBundle\Exception\AjaxRedirectResponseException
15+
- Contao\CoreBundle\Exception\InsufficientAuthenticationException
16+
- Contao\CoreBundle\Exception\InvalidRequestTokenException
17+
- Contao\CoreBundle\Exception\NoActivePageFoundException
18+
- Contao\CoreBundle\Exception\NoContentResponseException
19+
- Contao\CoreBundle\Exception\PageNotFoundException
20+
- Contao\CoreBundle\Exception\RedirectResponseException
21+
- Contao\CoreBundle\Exception\ResponseException
22+
- Contao\CoreBundle\Exception\ServiceUnavailableException
23+
- Contao\UnusedArgumentsException
24+
- Symfony\Component\Console\Exception\CommandNotFoundException
25+
- Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
26+
- Symfony\Component\HttpKernel\Exception\NotFoundHttpException
27+
- Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
28+
- Symfony\Component\Security\Core\Exception\AccessDeniedException
29+
30+
monolog:
31+
handlers:
32+
sentry:
33+
type: sentry
34+
level: !php/const Monolog\Logger::ERROR
35+
hub_id: Sentry\State\HubInterface
36+
37+
services:
38+
Monolog\Processor\PsrLogMessageProcessor:
39+
tags: { name: monolog.processor, handler: sentry }

‎src/ContaoManager/Plugin.php

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,32 @@
22

33
declare(strict_types=1);
44

5-
namespace Oneup\Contao\SentryBundle\ContaoManager;
5+
namespace Oneup\ContaoSentryBundle\ContaoManager;
66

77
use Contao\CoreBundle\ContaoCoreBundle;
88
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
99
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
1010
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
11-
use Contao\ManagerPlugin\Dependency\DependentPluginInterface;
12-
use Oneup\Contao\SentryBundle\OneupContaoSentryBundle;
11+
use Contao\ManagerPlugin\Config\ConfigPluginInterface;
12+
use Oneup\ContaoSentryBundle\OneupContaoSentryBundle;
1313
use Sentry\SentryBundle\SentryBundle;
14+
use Symfony\Component\Config\Loader\LoaderInterface;
1415

15-
class Plugin implements BundlePluginInterface, DependentPluginInterface
16+
class Plugin implements BundlePluginInterface, ConfigPluginInterface
1617
{
1718
public function getBundles(ParserInterface $parser): array
1819
{
1920
return [
20-
// load SentryBundle (dependency)
21-
BundleConfig::create(SentryBundle::class)->setLoadAfter([ContaoCoreBundle::class]),
22-
23-
// load OneupContaoSentryBundle
21+
BundleConfig::create(SentryBundle::class),
2422
BundleConfig::create(OneupContaoSentryBundle::class)->setLoadAfter([
2523
ContaoCoreBundle::class,
2624
SentryBundle::class,
2725
]),
2826
];
2927
}
3028

31-
public function getPackageDependencies(): array
29+
public function registerContainerConfiguration(LoaderInterface $loader, array $managerConfig): void
3230
{
33-
return [
34-
'sentry/sentry-symfony',
35-
];
31+
$loader->load(__DIR__.'/../../config/skeleton.yaml');
3632
}
3733
}

‎src/DependencyInjection/OneupContaoSentryExtension.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Oneup\Contao\SentryBundle\DependencyInjection;
5+
namespace Oneup\ContaoSentryBundle\DependencyInjection;
66

77
use Symfony\Component\Config\FileLocator;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -18,7 +18,7 @@ class OneupContaoSentryExtension extends Extension
1818
*/
1919
public function load(array $configs, ContainerBuilder $container): void
2020
{
21-
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22-
$loader->load('services.yml');
21+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
22+
$loader->load('services.yaml');
2323
}
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Oneup\ContaoSentryBundle\Integration;
6+
7+
use Sentry\Event;
8+
use Sentry\EventHint;
9+
use Sentry\SentrySdk;
10+
11+
class IgnorePreviousExceptionsIntegration
12+
{
13+
public function __invoke(Event $event, ?EventHint $hint): ?Event
14+
{
15+
if (!($exception = $hint?->exception) instanceof \Throwable) {
16+
return $event;
17+
}
18+
19+
$sentry = SentrySdk::getCurrentHub();
20+
21+
foreach ($sentry->getClient()?->getOptions()->getIgnoreExceptions() ?? [] as $class) {
22+
while ($exception && $exception = $exception->getPrevious()) {
23+
if (\is_a($exception, $class)) {
24+
return null;
25+
}
26+
}
27+
}
28+
29+
return $event;
30+
}
31+
}

‎src/OneupContaoSentryBundle.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace Oneup\Contao\SentryBundle;
5+
namespace Oneup\ContaoSentryBundle;
66

77
use Symfony\Component\HttpKernel\Bundle\Bundle;
88

99
class OneupContaoSentryBundle extends Bundle
1010
{
11+
public function getPath(): string
12+
{
13+
return \dirname(__DIR__);
14+
}
1115
}

‎src/Resources/config/services.yml

-9
This file was deleted.

‎src/Twig/ContaoSentryTwigExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Oneup\Contao\SentryBundle\Twig;
5+
namespace Oneup\ContaoSentryBundle\Twig;
66

77
use Twig\Extension\AbstractExtension;
88
use Twig\TwigFilter;

‎src/Twig/ContaoSentryTwigRuntime.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Oneup\Contao\SentryBundle\Twig;
5+
namespace Oneup\ContaoSentryBundle\Twig;
66

77
use Sentry\SentrySdk;
88

@@ -22,15 +22,9 @@ public function sentryLastEventIdFilter(): ?string
2222
public function sentryDsn(): ?string
2323
{
2424
if ((null !== $client = SentrySdk::getCurrentHub()->getClient())
25-
&& (null !== $dsn = $client->getOptions()->getDsn())) {
26-
27-
return sprintf(
28-
'%s://%s@%s/%s',
29-
$dsn->getScheme(),
30-
$dsn->getPublicKey(),
31-
$dsn->getHost(),
32-
$dsn->getProjectId()
33-
);
25+
&& (null !== $dsn = $client->getOptions()->getDsn())
26+
) {
27+
return (string) $dsn;
3428
}
3529

3630
return null;

0 commit comments

Comments
 (0)
Please sign in to comment.