crassula/google-cloud-error-reporting-bundle
Symfony bundle integrating Google Cloud Error Reporting. Configure project ID, service name and credentials, enable per environment, then report exceptions via the ErrorReporter service with optional HTTP request/response and user context.
Install the Bundle
composer require crassula/google-cloud-error-reporting-bundle
Ensure your composer.json includes the PECL extensions (grpc, protobuf) for optimal performance.
Register the Bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
Crassula\Bundle\GoogleCloudErrorReportingBundle\CrassulaGoogleCloudErrorReportingBundle::class => ['all' => true],
];
Configure in config/packages/crassula_google_cloud_error_reporting.yaml
crassula_google_cloud_error_reporting:
enabled: true
project_id: "your-gcp-project-id"
service: "your-service-name"
client_options:
credentials: "%kernel.project_dir%/config/gcp_credentials.json"
First Use Case: Manual Error Reporting
Inject ErrorReporter and report exceptions explicitly:
use Crassula\Bundle\GoogleCloudErrorReportingBundle\Service\ErrorReporter;
try {
$this->riskyOperation();
} catch (\Exception $e) {
$this->errorReporter->report($e, [
'http_request' => $request,
'user' => $user->getEmail(),
]);
}
Automatic Error Reporting (Recommended)
Enable use_listeners: true in config to auto-capture:
kernel.exception (web)console.error (CLI)
Reports trigger on kernel.terminate/console.terminate.Conditional Reporting
Use ignore_exceptions to exclude specific exceptions (e.g., ValidationFailedHttpException):
crassula_google_cloud_error_reporting:
ignore_exceptions:
- Symfony\Component\HttpKernel\Exception\ValidationException
Contextual Data Attach metadata to errors via the second argument:
$this->errorReporter->report($e, [
'http_response_status_code' => 500,
'user' => $user->getId(),
'request_options' => [
'retrySettings' => new \Google\ApiCore\RetrySettings(['maxAttempts' => 3]),
],
]);
Symfony Monolog Integration
Log failed reports to Monolog (default behavior) by configuring the logger option:
crassula_google_cloud_error_reporting:
logger: "monolog.logger.error_reporting"
Environment-Specific Configs
Disable in config/packages/dev/crassula_google_cloud_error_reporting.yaml:
crassula_google_cloud_error_reporting:
enabled: false
Dependency Injection
Tag ErrorReporter as a service for autowiring:
services:
Crassula\Bundle\GoogleCloudErrorReportingBundle\Service\ErrorReporter:
tags: ['container.service_subscriber']
Custom Error Groups
Use the service_version config to segment errors by deployment:
crassula_google_cloud_error_reporting:
service_version: "1.2.3"
Authentication Failures
credentials are invalid or missing.php -r "require 'vendor/autoload.php'; $client = new \Google\Cloud\ErrorReporting\ErrorReportingClient(['credentials' => 'path/to/key.json']);"
logger to log auth errors:
crassula_google_cloud_error_reporting:
logger: "monolog.logger.error_reporting"
Performance Overhead
crassula_google_cloud_error_reporting:
use_listeners: false
Duplicate Reports
ignore_exceptions or implement a ReportedException decorator:
// src/Service/ReportedException.php
class ReportedException extends \Exception {}
PHP 7.4+ Requirement
TypeError on native type hints.v0.7.0 (pre-7.4) if needed.Check Reported Errors
Use the Google Cloud Console to verify errors appear.
Filter by service and service_version in config.
Local Testing
Mock the ErrorReporter in tests:
$this->container->set(ErrorReporter::class, $this->createMock(ErrorReporter::class));
Config Validation Run:
bin/console debug:config crassula_google_cloud_error_reporting
To validate your configuration.
Custom Error Reporter
Implement Crassula\Bundle\GoogleCloudErrorReportingBundle\Service\ErrorReporterInterface:
class CustomErrorReporter implements ErrorReporterInterface {
public function report(\Throwable $exception, array $options = []): void {
// Custom logic (e.g., Slack alerts)
}
}
Register as a service and override the bundle’s default.
Event Subscribers
Extend the kernel.exception event to enrich data:
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
$subscriber = new class {
public function onKernelException(ExceptionEvent $event) {
$exception = $event->getThrowable();
$event->getRequest()->attributes->set('gcp_error_group', 'api-failure');
}
};
Retry Logic
Configure retry settings in client_options:
crassula_google_cloud_error_reporting:
client_options:
retrySettings:
maxAttempts: 5
initialDelay: 100ms
How can I help you explore Laravel packages today?