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 @@ -11,10 +11,13 @@

namespace AppBundle\EventListener;

use Doctrine\DBAL\Exception\DriverException;
use Symfony\Component\Console\ConsoleEvents;
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 +27,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 +39,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 +62,21 @@ 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)
{
$exception = $event->getException();
// Since any exception thrown during a Twig template rendering is wrapped in a Twig_Error_Runtime.
// We must get the original exception.
$previousException = $exception->getPrevious();

if ($previousException instanceof DriverException && !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.

the driver exception may still be the main exception depending on where it happens (it can happen in the controller)

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