What is the best way to handle errors from Mojo::Pg? (When using
non-blocking requests)
Example:
use Mojolicious::Lite;
use Mojo::Pg;
helper pg => sub { state $pg = Mojo::Pg->new('postgresql://
127.0.0.1/test?RaiseError=0') };
app->pg->db->query('select version()' => sub {
my ($db, $err, $res) = @_;
return app->log->error("Error while select version: $err") if $err;
print $res->array->[0];
});
app->start;
If database is not available, I get an error and application does not start:
Can't load application from file "app.pl": Can't call method "prepare" on
an undefined value at /usr/local/share/perl/5.18.2/Mojo/Pg/Database.pm line
106, <DATA> line 2231.
If I use option 'RaiseError=1', then application does not start anyway. I
can handle this using eval, but this is not very convenient.
I was hoping that I could handle this error through the error event (as in
Mojo::UserAgent when the error is not bound to a tx), but it seems not
working:
app->pg->on(error => sub {
my ($pg, $error) = @_;
app->log->error("Database error: $error");
});
--
Andrey Khozov
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.