Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Exception handling

Started by Richard Hainsworth 8 years ago · 1 replies · 1144 views
8 years ago

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:

PHP
// 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:

TXT
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
8 years ago

Found solution.
Grav uses namespaces. So catch blocks need \Exception, not Exception.

👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 46 1 week ago
Plugins · by Xavier, 4 weeks ago
2 55 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 49 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago