Installation:
composer require elao/error-notifier-bundle:dev-master
Register the bundle in config/bundles.php:
Elao\ErrorNotifierBundle\ElaoErrorNotifierBundle::class => ['all' => true],
Configuration:
Add to config/packages/elao_error_notifier.yaml:
elao_error_notifier:
recipients: ['admin@example.com']
# Optional: Customize email subject, template, or transport
First Use Case:
Trigger a 500 error (e.g., throw new \Exception('Test error') in a controller). Verify the email arrives with:
Error Handling Integration:
kernel.exception (for HTTP errors) and kernel.terminate (for PHP fatal errors).
// config/services.yaml
services:
App\EventListener\ErrorNotifierListener:
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception', method: 'onKernelException' }
Elao\ErrorNotifierBundle\Notifier\NotifierInterface to filter errors or enrich payloads.Email Customization:
templates/elao_error_notifier/email.html.twig).setTemplateVariables() in a custom notifier service.Transport Flexibility:
mailer component for async sending:
elao_error_notifier:
transport: 'async'
NotifierInterface.elao_error_notifier:
environments: ['prod', 'staging'] # Only notify in these
symfony/cache).Elao\ErrorNotifierBundle\Notifier\Notifier:
protected function getRequestData(Request $request) {
$data = parent::getRequestData($request);
unset($data['password']); // Example: Redact sensitive fields
return $data;
}
Archived Package:
Configuration Overrides:
elao_error_notifier.yaml is loaded after default configs to avoid precedence issues.monolog logs for Elao\ErrorNotifierBundle entries.Fatal Errors:
register_shutdown_function as a fallback:
register_shutdown_function(function() {
if (($error = error_get_last()) && in_array($error['type'], [E_ERROR, E_PARSE])) {
// Log or notify manually
}
});
Email Delivery:
swiftmailer (or mailer) is configured in config/packages/mailer.yaml.php bin/console debug:mailer:send to confirm transport works.elao_error_notifier:
debug: true # Logs errors to Symfony's logger
Elao\ErrorNotifierBundle\Notifier\Notifier to dump payloads:
public function notify(Error $error) {
file_put_contents('/tmp/error_notifier_debug.log', print_r($error->getDetails(), true));
parent::notify($error);
}
symfony/debug, twig, and swiftmailer are installed (required for full functionality).Custom Notifiers:
Implement NotifierInterface to add channels (e.g., SMS, PagerDuty):
class SlackNotifier implements NotifierInterface {
public function notify(Error $error) {
// Send to Slack webhook
}
}
Register in services.yaml:
services:
App\Notifier\SlackNotifier:
tags: [elao_error_notifier.notifier]
Dynamic Recipients: Use a service to resolve recipients dynamically:
elao_error_notifier:
recipient_resolver: 'app.error_recipient_resolver'
class ErrorRecipientResolver {
public function resolve(Error $error) {
return ['team-' . $error->getType() . '@example.com'];
}
}
Template Inheritance: Extend the base template to add company branding:
{# templates/elao_error_notifier/email.html.twig #}
{% extends '@ElaoErrorNotifier/email.html.twig' %}
{% block header %}
{{ parent() }}
<img src="{{ asset('images/logo.png') }}" alt="Company Logo">
{% endblock %}
How can I help you explore Laravel packages today?