Hey <@U01PL2YSG8L>, and how about handling errors ...
# community-help
s
Hey @Kishore Nallan, and how about handling errors when connecting to the Typesense server? I've noticed today, as I've started my Express server before the Typesense server, I've got this error, and it crashed my server:
Copy code
Request #1709883731535: Request to Node 0 failed due to "ECONNREFUSED "
Request #1709883731535: Sleeping for 5s and then retrying request...
Request #1709883727700: Request to Node 0 failed due to "ECONNREFUSED "
Request #1709883727700: Sleeping for 5s and then retrying request...
Request #1709883731535: Request to Node 0 failed due to "ECONNREFUSED "
Request #1709883731535: Sleeping for 5s and then retrying request...
AggregateError:
    at internalConnectMultiple (node:net:1114:18)
    at afterConnectMultiple (node:net:1667:5)
[ERROR] 09:42:17 AggregateError
So I was wondering how connection errors are handled, I couldn't find anything about this in the docs, and looking at the TS types, also there is not very clear path for it. The only way so far that I figured out is to handle it in the functions where I'm actually using
typesenseClient
instance. But I'm wondering if there is some way to know if the connection is made succesfuly or not when I make
new Typesense.Client...
? Here is how I make connection:
Copy code
import Typesense, { Client } from "typesense";
const apiKey = process.env.TYPESENSE_API_KEY;

let typesenseClient: Client | undefined;
if (apiKey)
  typesenseClient = new Typesense.Client({
    nodes: [{ host: "localhost", port: 8108, protocol: "http" }],
    apiKey: apiKey,
    connectionTimeoutSeconds: 2,
    retryIntervalSeconds: 1,
    numRetries: 1
  });

export default typesenseClient;