簡體   English   中英

如何在Perl MongoDB驅動程序上捕獲連接錯誤?

[英]How to catch connection error on Perl MongoDB driver?

我正在使用正式的Perl驅動程序來使用mongodb。 捕獲並處理錯誤, Try::TinySafe::Isa模塊建議 但是,它不能按預期工作。 請檢查下面的代碼,該代碼應根據文檔工作,但實際上不起作用:

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字符串來強制連接,但同樣也不例外。 我在沒有安裝mongodb且沒有mongodb docker容器運行的機器上運行此腳本,因此肯定會出現異常。

有人可以解釋我在做什么錯嗎?

這是一個微妙的問題。 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM