How do I catch a MongoSecurityException?

前端 未结 1 1777
说谎
说谎 2021-01-26 15:51

I\'m trying to validate the login details of a specific user here.

This just won\'t work. I have no idea why, it never reaches the catch block even though there is a

相关标签:
1条回答
  • 2021-01-26 16:21

    You cannot catch MongoSecurityException as it is thrown in a background thread.

    You can wait for a MongoTimeoutException to handle 'synchronously':

      MongoClientOptions clientOptions = new MongoClientOptions.Builder().serverSelectionTimeout(500).build();
        mongoClient = new MongoClient(serverAddress, Collections.singletonList(credential), clientOptions);
        try {
            String address = mongoClient.getConnectPoint();
            System.out.println(address);
        }catch (Throwable e){
            System.out.println(e);
        }
    

    Or you can implement a ServerListener and handle asynchronously

    { 
    MongoClientOptions clientOptions = new MongoClientOptions.Builder().addServerListener(this).build();
    mongoClient = new MongoClient(host1, Collections.singletonList(credential), clientOptions);
    }
    
    @Override
    public void serverDescriptionChanged(ServerDescriptionChangedEvent event) {
        Throwable exception = event.getNewDescription().getException();
        handle(exception);
    }
    
    0 讨论(0)
提交回复
热议问题