generated from swisnl/skeleton-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup project for mautic with basic sentry impl.
- Loading branch information
TBiesenbeek
committed
Nov 15, 2024
1 parent
0b3dada
commit 54f88fd
Showing
5 changed files
with
99 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters