mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
99 stars 46 forks source link

Actual error information #20

Closed avkhozov closed 8 years ago

avkhozov commented 8 years ago

Any invalid database query raise exception from Mojo::Pg::Database file and it's hard to find actual file with error.

$ cat error.pl 
#!/usr/bin/perl

use Mojo::Base -strict;
use Mojo::Pg;

my $pg = Mojo::Pg->new('postgresql://and@/m');

say $pg->db->query('table x')->text;

Expected behavior

$ perl -Ilib/ error.pl 
DBD::Pg::st execute failed: ERROR:  relation "x" does not exist
LINE 1: table x
              ^ at error.pl line 8.
$ 

Actual behavior

$ perl error.pl
DBD::Pg::st execute failed: ERROR:  relation "x" does not exist
LINE 1: table x
              ^ at /usr/local/share/perl/5.22.1/Mojo/Pg/Database.pm line 78, <DATA> line 2125.
$ 

Patch

diff --git a/lib/Mojo/Pg/Database.pm b/lib/Mojo/Pg/Database.pm
index b0727d4..081ad1f 100644
--- a/lib/Mojo/Pg/Database.pm
+++ b/lib/Mojo/Pg/Database.pm
@@ -75,6 +75,7 @@ sub query {
   $attrs{pg_placeholder_dollaronly} = 1        if delete $self->{dollar_only};
   $attrs{pg_async}                  = PG_ASYNC if $cb;
   my $sth = $self->dbh->prepare_cached($query, \%attrs, 3);
+  $sth->{HandleError} = sub { croak shift } if $sth->{RaiseError};
   $sth->execute(map { _json($_) ? to_json $_->{json} : $_ } @_);

   # Blocking
pink-mist commented 8 years ago

I definitely :+1: this ... having more accurate error information helps a lot

anparker commented 8 years ago

It's simple and can save some time searching for broken queries after recent schema update. :) :+1:

kraih commented 8 years ago

Thanks, i went with a slightly different solution.

avkhozov commented 8 years ago

@kraih thanks.