nestjsmilvus

Exception is not catched in NestJS


The following is my class:

import {
  Injectable
} from '@nestjs/common';
import {
  MilvusClient
} from '@zilliz/milvus2-sdk-node';

@Injectable()
export class EmbeddingService {
  private client: MilvusClient;

  constructor() {
    try {
      this.client = new MilvusClient('localhost:19530');
    } catch (error) {
      console.error('Failed to connect to Milvus:', error);
    }
  }
}

My problem is that when I stop the milvus service (docker container) the above new MilvusClient command raises an error but the error is not catched by the catch block.

I tried to wrap the try-catch inside an IIFE but the same problem and the catch block does not work.


Solution

  • I solved the error with the following approach:

    const milvusClient = new MilvusClient(`invalid-address`);
    
    milvusClient.connectPromise.catch((e)=>{
      console.log('connect failed')
    });