njlg / perl-rethinkdb

A Pure Perl RethinkDB Driver
http://njlg.info/perl-rethinkdb
Artistic License 2.0
22 stars 5 forks source link

wrong auth_key dies #18

Closed grantwork closed 8 years ago

grantwork commented 9 years ago

If I use an incorrect auth_key in a query, the script dies with the below error, so I'm unable to catch the error in my app and notify admins.

send: Cannot determine peer address at _config/lib/share/Rethinkdb/IO.pm line 286

njlg commented 9 years ago

Thanks for reporting the issue. I'll look into this later today.

grantwork commented 9 years ago

Thanks for jumping on this so quickly.

Just an FYI, my app needs to catch a failed connection or failed query so it can fail gracefully. So I had to modify the IO.pm file to return undef on failed connection instead of croaking. It would be nice to have this feature native in the class so we can catch everything within the app.

  $self->{_handle} = IO::Socket::INET->new(
    PeerHost => $self->host,
    PeerPort => $self->port,
    Reuse    => 1,
    Timeout  => $self->timeout,
    )
    or return undef; 
    # Modified this as we can't just croak
    #or croak 'ERROR: Could not connect to ' . $self->host . ':' . $self->port;
njlg commented 9 years ago

Can you give me a quick example of how you have RethinkDB setup and how you are connecting to it?

I started RethinkDB 2.1.3 and gave it an auth_key. Then when I connect to it with no auth_key or a different one, I get the following error:

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /.../perl-5.22.0/lib/site_perl/5.22.0/Rethinkdb/IO.pm line 206.

Is that what you see?

njlg commented 9 years ago

I actually found the problem. There was a typo in IO.pm that masked the connection error. I'm surprised that it hasn't caused any problems before.

You can try to pull down my next_version branch or just wait a bit and I'll push an update to CPAN.

grantwork commented 9 years ago

I'm using my modified IO.pm file that returns undef as follows.

sub rethinkdb_connect
{
    use Rethinkdb;

    $::gbl{REF}->{'rethinkdb'} = r->connect($host, $port, $database, $authKey, 5);

    if(!defined($::gbl{REF}->{'rethinkdb'})){
        functions::emailAdmin( type => 'technical',
            subject => "Could not connect to rethink DB", 
            message => "$host, $port, $database" 
        );
    }

    return $::gbl{REF}->{'rethinkdb'};
}

if( !$::gbl{REF}->{'rethinkdb'} ){
    return if !rethinkdb_connect();
}

Great, thanks for fixing that, I'll pull down that version.

njlg commented 9 years ago

It look me a little bit longer to get the release out (I had to bring my test coverage back up), but I just uploaded a new version to CPAN. The new version (0.11) should start showing up later today: https://metacpan.org/pod/Rethinkdb