acseo/aierrorexplained-bundle
Symfony dev bundle that enhances the default error page with AI-powered explanations and fix suggestions using OpenAI. Includes caching to avoid repeat calls for the same exception and integrates via a custom runtime error handler and error controller.
composer require --dev acseo/aierrorexplained-bundle
composer.json (add runtime error handler):
"extra": {
"runtime": {
"error_handler": "ACSEO\\AIErrorExplainedBundle\\Runtime\\Internal\\AIErrorHandler"
}
}
.env:
OPENAI_CLIENT_KEY=sk-your-key-here
# config/packages/framework.yml
when@dev:
framework:
error_controller: ACSEO\AIErrorExplainedBundle\Controller\ErrorController::show
1/0 in a dev controller) to see AI-generated explanations.Debugging a runtime exception (e.g., UndefinedMethodException, DatabaseException) with an AI-generated fix suggestion in the Symfony error page.
Exception Handling:
AIErrorHandler, which intercepts all uncaught exceptions.ErrorHandlerInterface to process exceptions before rendering.AI Query Generation:
"You are a Symfony developer. Explain how to fix this error:
Exception: [Class], Message: [Message], Trace: [Trace].
Provide a concise fix (code snippet + explanation)."
Caching Layer:
cache:app) to avoid redundant API calls for identical errors.aierror_explained_{exception_hash}.Error Page Integration:
error404.html.twig/error.html.twig) with an ai_suggestion block.dev: true in bundles.php) to avoid API costs and latency.# config/services.yaml
ACSEO\AIErrorExplainedBundle\Service\AIErrorService:
arguments:
$promptTemplate: "Your custom prompt here..."
Cache component to throttle OpenAI API calls (e.g., 1 call/minute).fallback_on_failure in config/packages/aierror_explained.yaml).// In a controller
try {
$entityManager->flush();
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) {
// Exception bubbles up to AIErrorHandler
throw $e; // AI will suggest fixing the foreign key constraint
}
API Costs:
Rate Limits:
CacheInterface) to debounce API calls.Sensitive Data Exposure:
DATABASE_URL).// Override AIErrorService::generatePrompt()
$sanitizedTrace = preg_replace('/password|secret|key=[^&]+/', '[REDACTED]', $trace);
Cache Invalidation:
last_updated timestamp to cache keys and invalidate periodically.Bundle Maturity:
HttpException, LogicException) before relying on it.# config/packages/aierror_explained.yaml
ai_error_explained:
debug: true
OpenAI\Client service:
// tests/Service/AIErrorServiceTest.php
$client = $this->createMock(OpenAI\Client::class);
$client->method('chat')->willReturn(['choices' => [['message' => ['content' => 'Mock fix']]]]);
$service = new AIErrorService($client, $cache);
bin/console cache:pool:list
bin/console cache:pool:clear aierror_explained
Custom Error Handling:
Override AIErrorHandler to exclude specific exceptions:
// src/EventSubscriber/AIErrorSubscriber.php
public function onKernelException(GetResponseForExceptionEvent $event) {
$exception = $event->getThrowable();
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return; // Skip AI for 404s
}
// ... rest of logic
}
Prompt Customization:
Extend AIErrorService to add domain-specific context:
public function generatePrompt(Throwable $exception): string {
$basePrompt = parent::generatePrompt($exception);
return "{$basePrompt}\n\nNote: This is a Symfony 6.4 app with Doctrine ORM.";
}
Alternative AI Providers:
Replace OpenAI with a local LLM (e.g., Hugging Face) by injecting a custom AIClientInterface:
services:
ACSEO\AIErrorExplainedBundle\Service\AIClientInterface: '@your_local_ai_service'
Error Page Styling:
Customize the Twig template (templates/error.html.twig) to highlight AI suggestions:
<div class="ai-suggestion">
<h3>AI Suggests:</h3>
{{ ai_suggestion|raw }}
</div>
How can I help you explore Laravel packages today?