Skip to content

Commit

Permalink
Merge pull request #1 from swisnl/feature/init-buindle
Browse files Browse the repository at this point in the history
feat: setup project for mautic with basic sentry impl.
  • Loading branch information
MakerTim authored Nov 15, 2024
2 parents 0b3dada + 54f88fd commit 1addf46
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 54 deletions.
53 changes: 0 additions & 53 deletions .github/workflows/tests.yml

This file was deleted.

38 changes: 38 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

return [
'name' => 'Mautic Sentry Error handler',
'description' => 'Logs errors to Sentry.',
'version' => '1.0',
'author' => 'SWIS',

'services' => [
'other' => [
'mautic_sentry.event_listener' => [
'class' => MauticPlugin\MauticSentryBundle\EventListener\SentryExceptionListener::class,
'arguments' => [],
'tags' => [
'kernel.event_listener',
'kernel.event_listener',
],
'tagArguments' => [
[
'event' => 'kernel.exception',
'method' => 'handleExceptionEvent',
/*
* 255 is the Mautic error-page priority, we want to log error's before it stops propagation.
* @see app/bundles/CoreBundle/Config/config.php
*/
'priority' => 256,
],
[
'event' => 'kernel.terminate',
'method' => 'handleKernelTerminateEvent',
],
],
],
],
],
];
44 changes: 44 additions & 0 deletions EventListener/SentryExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\MauticSentryBundle\EventListener;

use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;

final class SentryExceptionListener
{
public function __construct()
{
if (!getenv('SENTRY_DSN')) {
return;
}

\Sentry\init([
'dsn' => getenv('SENTRY_DSN'),
'environment' => getenv('SENTRY_ENVIRONMENT') ?: getenv('APP_ENV') ?: 'unknown',
]);
}

public function handleExceptionEvent(ExceptionEvent $event): void
{
\Sentry\captureException($event->getThrowable());
$event->getRequest()->attributes->set(SentryExceptionListener::class, true);
}

public function handleKernelTerminateEvent(TerminateEvent $event): void
{
$request = $event->getRequest();
$response = $event->getResponse();

// If already logged the exception, we don't log the Termination event.
if ($request->attributes->get(SentryExceptionListener::class, false)) {
return;
}

if ($response->getStatusCode() < 200 || $response->getStatusCode() > 299) {
\Sentry\captureException(new \RuntimeException(sprintf('Terminated route @ "%s"', $request->getRequestUri())));
}
}
}
11 changes: 11 additions & 0 deletions MauticSentryBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\MauticSentryBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

class MauticSentryBundle extends PluginBundleBase
{
}
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0"
"php": "^8.0",
"mautic/core-lib": "^5.0",
"sentry/sentry": "^4.10"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
Expand All @@ -32,6 +36,7 @@
"fix-style": "php-cs-fixer fix"
},
"extra": {
"install-directory-name": "MauticSentryBundle",
"branch-alias": {
"dev-master": "1.0-dev"
}
Expand Down

0 comments on commit 1addf46

Please sign in to comment.