Hi again, have you tried the documented way to access the DBI error, by using a hook?
database_error
Called when a database error is raised by DBI. Receives two parameters: the error message being returned by DBI, and the database handle in question.
(See https://metacpan.org/pod/Dancer2::Plugin::Database#HOOKS)
Edit: I spun up a test:
Create a DB with no tables:
$ sqlite3 test.db
In config.yml:
plugins: Database: driver: SQLite database: 'test.db'
app.psgi:
#!/usr/bin/env perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use TestApp; TestApp->to_app;
TestApp.pm:
package TestApp; use Dancer2; use Dancer2::Plugin::Database; hook 'database_error' => sub { my $err = shift; warn "\nThe DB error: `$err`"; }; get '/' => sub { eval { database->quick_insert('no_such_table', { foo => 'bar' }) } +; 'Hello, world'; }; 1;
Log output:
(Note that the DB error is also printed to the log by default; you should be able to see that in your log and only need to use the hook if further processing is desired.)HTTP::Server::PSGI: Accepting connections at http://0:5000/ [TestApp:4783] core @2018-09-23 09:52:38> looking for get / in /home/n +ick/perl5/perlbrew/perls/perl-5.28.0/lib/site_perl/5.28.0/Dancer2/Cor +e/App.pm l. 36 [TestApp:4783] core @2018-09-23 09:52:38> Entering hook core.app.befor +e_request in (eval 305) l. 1 [TestApp:4783] debug @2018-09-23 09:52:38> Adding sqlite_unicode to DB +I connection params to enable UTF-8 support in /home/nick/perl5/perlb +rew/perls/perl-5.28.0/lib/site_perl/5.28.0/Dancer2/Plugin.pm l. 543 [TestApp:4783] core @2018-09-23 09:52:38> Entering hook plugin.databas +e.database_connected in (eval 305) l. 1 [TestApp:4783] core @2018-09-23 09:52:38> Entering hook plugin.databas +e.database_error in (eval 305) l. 1 The DB error: `DBD::SQLite::db do failed: no such table: no_such_table +` at /home/nick/dev/TestApp/bin/../lib/TestApp.pm line 11. DBD::SQLite::db do failed: no such table: no_such_table at /home/nick/ +perl5/perlbrew/perls/perl-5.28.0/lib/site_perl/5.28.0/Dancer/Plugin/D +atabase/Core/Handle.pm line 310. ...
Hope this helps!
In reply to Re: Dancer2::Plugin::Database and database errors (hook database_error)
by 1nickt
in thread Dancer2::Plugin::Database and database errors
by lee_crites
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |