Skip to content

Show better message when SQLite is not enabled #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
# subscriber includes a method that returns the list of listened events.
# See http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
app.console_subscriber:
class: AppBundle\EventListener\ConsoleEventSubscriber
class: AppBundle\EventListener\CheckSQLiteEventSubscriber
tags:
- { name: kernel.event_subscriber }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* This application uses by default an SQLite database to store its information.
Expand All @@ -24,7 +26,7 @@
*
* @author Javier Eguiluz <[email protected]>
*/
class ConsoleEventSubscriber implements EventSubscriberInterface
class CheckSQLiteEventSubscriber implements EventSubscriberInterface
{
// Event Subscribers must define this method to declare the events they
// listen to. You can listen to several events, execute more than one method
Expand All @@ -36,6 +38,8 @@ public static function getSubscribedEvents()
// Exceptions are one of the events defined by the Console. See the
// rest here: http://symfony.com/doc/current/components/console/events.html
ConsoleEvents::EXCEPTION => 'handleDatabaseExceptions',
// See: http://api.symfony.com/3.2/Symfony/Component/HttpKernel/KernelEvents.html
KernelEvents::EXCEPTION => 'onKernelException',
];
}

Expand All @@ -57,4 +61,16 @@ public function handleDatabaseExceptions(ConsoleExceptionEvent $event)
}
}
}

/**
* This method is triggered when kernel exception occurs. And checks if sqlite extension is enabled.
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (!extension_loaded('sqlite3')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be done only if the exception is a DBAL exception IMO. Otherwise, it could hide other issues happening earlier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i agree with that. But the problem is that from $event->getException() i get Twig_Error_Runtime and don't know why. Maybe is documented somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, any exception thrown during a Twig template rendering is wrapped in a Twig_Error_Runtime. You can check the chain of previous exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, didn't know that. Ok :)

$event->setException(new \Exception('PHP extension "sqlite3" must be enabled because, by default, the Symfony Demo application uses SQLite to store its information.'));
}
}
}