#Can't connect to my TCP microservice if I use a customClass

5 messages · Page 1 of 1 (latest)

nocturne holly
#

Hello! I want to serialize errors from my TCP service. I've setup a custom proxy. When I use it to connect to my microservice, I get a ECONNREFUSED error. When I don't, everything works. Is there a special way I should create my microservice so that the client can connect?

I've simplified the code of course, only adding the CLIENT app's module that connects to the service, and how I create the microservice in the SERVICE app. Thanks!

ERROR PROXY

export class TcpErrorHandlingProxy extends ClientTCP {
  serializeError(err: Error): RpcException {
    return new RpcException(err);
  }
}

CLIENT APP

@Module({
  imports: [ConfigModule.forRoot({ load: [serviceConfig] })],
  providers: [
    {
      provide: SERVICE_PACKAGE_TOKEN,
      useFactory: async (configService: ConfigService): Promise<ClientProxy> => {
        const { host, port }  = configService.get<serviceConfig>('service')!;
        const microserviceOptions: TcpClientOptions = { transport: Transport.TCP,  options: {  host, port } };

        return ClientProxyFactory.create(microserviceOptions); // <=  THIS WORKS
   //--------------------//     
         return ClientProxyFactory.create({ customClass: TcpErrorHandlingProxy, options: microserviceOptions }); //<= THIS DOES NOT WORK: ECONNREFUSED on connection
      },
      inject: [ConfigService],
    },
  ],
  exports: [SERVICE_PACKAGE_TOKEN],
})
export default class MicroserviceClientModule {}

SERVICE APP

  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    transport: Transport.TCP,
    options: {
      host,
      port,
      retryAttempts: 5,
      retryDelay: 10,
    },
  });
nocturne holly
#

I've been reading this https://docs.nestjs.com/microservices/custom-transport#client-proxy over and over, and I don't understand. What I do is only telling the client how to interpret errors, it should not affect the transport itself. Why can't I connect anymore without changing anything else? I can litterally comment out the line to make the transport work again.

nocturne holly
#

No one? 😭

dim harbor
#

Be sure to use a different port for HTTP and for TCP, that's the only thing I can think of now

#

But that's probably not it.. If you can, please create a minimal reproducible example (ideally in a github repo) so we can run and debug it locally.