Installation
composer require classmarkets/raven-bundle ~1.0.0
Add the bundle to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
new \ClassMarkets\RavenBundle\ClassMarketsRavenBundle(),
Enable Sentry Integration
Ensure your Symfony app is already configured with Sentry (e.g., via sentry/sentry-symfony). The bundle assumes Sentry is active and exceptions are being captured.
First Use Case
Modify your error template (e.g., templates/bundles/TwigBundle/Exception/error.html.twig) to include the event ID:
{% if exception is defined %}
{% set eventId = sentry_event_id(exception) %}
{% if eventId %}
<p>Error reported to Sentry: <code>{{ eventId }}</code></p>
{% endif %}
{% endif %}
Exception Handling
Use the Twig function sentry_event_id(exception) in error templates to display Sentry event IDs. Example:
{% extends 'base.html.twig' %}
{% block content %}
<h1>Error: {{ exception.message }}</h1>
{% set eventId = sentry_event_id(exception) %}
{% if eventId %}
<p>Sentry Event ID: <code>{{ eventId }}</code></p>
{% endif %}
{% endblock %}
Programmatic Access Retrieve event IDs in controllers/services via the service container:
$eventId = $this->get('cm_raven.sentry_event_recorder')->getEventIdForException($exception);
Custom Error Pages Extend the default error template to include the event ID in custom error pages (e.g., for APIs or user-friendly messages).
sentry/sentry-monolog, ensure the bundle is loaded after the Monolog bundle to avoid conflicts.# config/packages/twig.yaml
twig:
extensions:
- ClassMarkets\RavenBundle\Twig\SentryEventExtension
return $this->json([
'error' => $exception->getMessage(),
'sentry_event_id' => $this->get('cm_raven.sentry_event_recorder')->getEventIdForException($exception),
], 500);
No Event ID for Non-Sentry Exceptions
The bundle only works if the exception was captured by Sentry. If no event ID exists, sentry_event_id() returns null. Always check for its existence:
{% if sentry_event_id(exception) %}
{# Render event ID #}
{% endif %}
Symfony Version Compatibility
The bundle is designed for Symfony 2/3. If using Symfony 4+, ensure the cm_raven.sentry_event_recorder service is autowired correctly (it may require manual registration in config/services.yaml):
services:
cm_raven.sentry_event_recorder:
class: ClassMarkets\RavenBundle\Sentry\EventRecorder
arguments: ['@sentry.client']
Caching Issues If event IDs disappear after deployment, clear Symfony’s cache:
php bin/console cache:clear
config/packages/sentry.yaml or app/config/config.yml.$recorder = $this->get('cm_raven.sentry_event_recorder');
$this->logger->debug('Event ID for exception:', ['id' => $recorder->getEventIdForException($exception)]);
sentry_event_id function:
# config/packages/dev/twig.yaml
twig:
debug: true
Custom Event ID Storage
Override the EventRecorder service to store event IDs in a custom location (e.g., session or database):
// src/Service/CustomEventRecorder.php
class CustomEventRecorder extends \ClassMarkets\RavenBundle\Sentry\EventRecorder {
public function getEventIdForException(\Exception $exception) {
// Custom logic here
}
}
Then update services.yaml to use your class.
Twig Function Aliases
Add a shorter Twig alias for sentry_event_id in your app’s Twig config:
twig:
globals:
sentry_id: '@cm_raven.sentry_event_recorder'
Usage:
{{ sentry_id.getEventIdForException(exception) }}
Event ID in Logs Extend the bundle to log event IDs alongside exceptions using Monolog:
$this->get('monolog.logger')->info('Exception with Sentry ID', [
'exception' => $exception,
'sentry_id' => $recorder->getEventIdForException($exception),
]);
How can I help you explore Laravel packages today?