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 8, 2024
1 parent
0b3dada
commit c2fb44f
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
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' => 'Custom Sentry Error handler', | ||
'description' => 'Logs errors to Sentry.', | ||
'version' => '1.0', | ||
'author' => 'SWIS', | ||
|
||
'services' => [ | ||
'other' => [ | ||
'custom_sentry.event_listener' => [ | ||
'class' => MauticPlugin\SwisSentryBundle\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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MauticPlugin\SwisSentryBundle; | ||
|
||
use Mautic\PluginBundle\Bundle\PluginBundleBase; | ||
|
||
class CustomSentryBundle 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MauticPlugin\SwisSentryBundle\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('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