in reply to Dancer2::Plugin::Database and database errors

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:

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. ...
(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.)

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Dancer2::Plugin::Database and database errors (hook database_error)
by lee_crites (Scribe) on Sep 24, 2018 at 04:25 UTC

    Yes, I tried using the database_error hook. Here's the code:

    hook 'database_error' => sub { my $errstr = shift; my $dbh = shift; var errstr => $errstr; };

    I would then check for errstr, but it was never set. I checked three ways: 1) the way I showed here (which still is in the code), 2) by issuing a debug() call, and 3) having it die "some msg." None of them triggered. So it is as if the error is in the plugin, not in the database.

    I was going to try to debug it further to put in a but report, but, frankly, I built a workaround. I have two teams at work waiting for this to be working, so I didn't have the luxury of time.

    You also asked if I tried the debug; no I didn't. I should, and perhaps when the pressure is off and I can go through the code a little more to clean it up, I will -- I bookmarked this thread just in case suggestions like yours helped.

    Thanks for your help.

    David Lee Crites, Author and DevOps Dude
    Devops: lee@critesclan.com
    Author: davidlee@adoniah.co
    quad alii ut vivere