diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index a35f801..0000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,53 +0,0 @@ -name: Run tests - -on: [ push, pull_request ] - -jobs: - php-tests: - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - php: [ '7.4', '8.0', '8.1' ] - stability: [ prefer-lowest, prefer-stable ] - - name: P${{ matrix.php }} - ${{ matrix.stability }} - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - # Fetch 10 commits or Scrutinizer will throw - fetch-depth: 10 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - coverage: xdebug - tools: composer:v2 - - - name: Get composer cache directory - id: composer-cache - run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - - name: Cache composer dependencies - uses: actions/cache@v4 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: php-${{ matrix.php }}-composer-${{ matrix.stability }}-${{ hashFiles('**/composer.json') }} - restore-keys: php-${{ matrix.php }}-composer-${{ matrix.stability }}- - - - name: Install dependencies - run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - - - name: Execute tests - run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - - - name: Upload Scrutinizer coverage - uses: sudo-bot/action-scrutinizer@latest - # Do not run this step on forked versions of the main repository (example: contributor forks) - if: github.repository == 'swisnl/mautic-sentry-bundle' - with: - cli-args: "--format=php-clover coverage.clover --revision=${{ github.event.pull_request.head.sha || github.sha }}" diff --git a/Config/config.php b/Config/config.php new file mode 100644 index 0000000..b5a1d25 --- /dev/null +++ b/Config/config.php @@ -0,0 +1,38 @@ + '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', + ], + ], + ], + ], + ], +]; diff --git a/CustomSentryBundle.php b/CustomSentryBundle.php new file mode 100644 index 0000000..295d19a --- /dev/null +++ b/CustomSentryBundle.php @@ -0,0 +1,11 @@ + 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()))); + } + } +} diff --git a/composer.json b/composer.json index 6736780..a31ae35 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -32,6 +36,7 @@ "fix-style": "php-cs-fixer fix" }, "extra": { + "install-directory-name": "MauticSentryBundle", "branch-alias": { "dev-master": "1.0-dev" }