Exception is not catched in NestJS

63 views Asked by At

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.

2

There are 2 answers

0
Mohsen On BEST ANSWER

I solved the error with the following approach:

const milvusClient = new MilvusClient(`invalid-address`);

milvusClient.connectPromise.catch((e)=>{
  console.log('connect failed')
});
0
Micael Levi On

this has nothing to do with nestjs nor nodejs itself, it just how that MilvusClient was implemented. Looks like they didn't expose any way to handle errors on the constructor method (which is pretty bad)

I'd suggest you to report that in their repository: https://github.com/milvus-io/milvus-sdk-node/issues `