I am developing a plugin to push data to an sqlite database. The plugin works when SQL stanzas are correct. However, I want to create a soft fail when the SQL is not accepted by the SQLite3 php module. For example, when I try to add a row with a duplicate value in a column name that has a UNIQUE constraint.
I have SQLITE3::enableExceptions(true) set.
I am not a php guru, so I think I have missed something.
Here is the relevant snippet in the ‘OnFormProcessed’ method of my plugin:
// extracted from sqlite.php:
// ...
$sql ="INSERT INTO {$params['table']} ( $fields ) VALUES ( $values )";
$this->grav['debugger']->addMessage("sql: $sql");
$db = $this->grav['sqlite']['db']; // this is line 78
try {
$db->exec($sql) ;
} catch (Exception $e) {
$mess = SQLite3::lastErrorMsg();
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $e->getMessage()
]));
$event->stopPropagation();
}
When I use a Form with the custom action and the data is unique, then the action works correctly and the data is added to the database.
However, when I try a Form with duplicate data, and I am expecting an Exception, GRAV blows back with an Error page. But I thought I had correctly coded to catch the Exception.
This perplexes me. What have I done wrong?
Have I used the correct technique (fire a onFormValidationError) to return an error message back to the user?
The error trace in Grav is:
Exception thrown with message "UNIQUE constraint failed: clients.idserial, clients.surname, clients.name"
Stacktrace:
#20 Exception in /var/www/html/mobilet/user/plugins/sqlite/sqlite.php:78
#19 SQLite3:exec in /var/www/html/mobilet/user/plugins/sqlite/sqlite.php:78
#18 Grav\Plugin\SqlitePlugin:onFormProcessed in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:184
#17 call_user_func in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:184
#16 Symfony\Component\EventDispatcher\EventDispatcher:doDispatch in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:46
#15 Symfony\Component\EventDispatcher\EventDispatcher:dispatch in /var/www/html/mobilet/vendor/rockettheme/toolbox/Event/src/EventDispatcher.php:23
#14 RocketTheme\Toolbox\Event\EventDispatcher:dispatch in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:288
#13 Grav\Common\Grav:fireEvent in /var/www/html/mobilet/user/plugins/form/classes/form.php:642
#12 Grav\Plugin\Form:post in /var/www/html/mobilet/user/plugins/form/form.php:182
#11 Grav\Plugin\FormPlugin:onPagesInitialized in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:184
#10 call_user_func in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:184
#9 Symfony\Component\EventDispatcher\EventDispatcher:doDispatch in /var/www/html/mobilet/vendor/symfony/event-dispatcher/EventDispatcher.php:46
#8 Symfony\Component\EventDispatcher\EventDispatcher:dispatch in /var/www/html/mobilet/vendor/rockettheme/toolbox/Event/src/EventDispatcher.php:23
#7 RocketTheme\Toolbox\Event\EventDispatcher:dispatch in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:288
#6 Grav\Common\Grav:fireEvent in /var/www/html/mobilet/system/src/Grav/Common/Processors/PagesProcessor.php:25
#5 Grav\Common\Processors\PagesProcessor:process in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:132
#4 Grav\Common\Grav:Grav\Common\{closure} in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:379
#3 Grav\Common\Grav:Grav\Common\{closure} in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:355
#2 call_user_func_array in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:355
#1 Grav\Common\Grav:__call in /var/www/html/mobilet/system/src/Grav/Common/Grav.php:133
#0 Grav\Common\Grav:process in /var/www/html/mobilet/index.php:52