How to catch connection error on Perl MongoDB?

I am using the official Perl driver to work with mongodb. To catch and handle errors, Try::Tinyand Safe::Isamodules are recommended . However, it does not work properly. Please check the code below, which should work in accordance with the documentation, but actually it does not work:

use MongoDB;
use Try::Tiny;
use Safe::Isa;

my $client;

try {
    $client = MongoDB->connect('mongodb://localhost');
    $client->connect;
} catch {
    warn "caught error: $_";
};

my $collection = $client->ns('foo.bar');

try {
  my $all = $collection->find;
} catch {
  warn "2 - caught error: $_";;
};

, connect() . ! , $client->connect force, . script , mongodb, - mongodb, .

- , ?

+4
1

. find , . MongoDB::Collection:

Note, a MongoDB::Cursor object holds the query and does not issue the
query to the server until the `result` method is called on it or until
an iterator method like `next` is called.
+1

Source: https://habr.com/ru/post/1677555/


All Articles