[英]Ruby mongo driver: Catch MongoDB connection errors after a few seconds?
[英]How to catch connection error on Perl MongoDB driver?
我正在使用正式的Perl驅動程序來使用mongodb。 捕獲並處理錯誤, Try::Tiny
和Safe::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.