deepgram / deepgram-js-sdk

Official JavaScript SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
127 stars 45 forks source link

Some of Websocket errors still go unhandled, even if move the "error" & "close" listeners above the "open" listener (to handle events before opening the client) #300

Open Ivan-juni opened 3 weeks ago

Ivan-juni commented 3 weeks ago

Hi dear all, Allow me to re-open this issue as I believe it is still present in the Deepgram JS SDK - after a good amount of debugging the issue. It is easy reproducible using the server implementation here - https://github.com/deepgram-devs/node-live-example/blob/main/server.js

It turned out that my API key has expired - however, this is not the root cause of the issue. If attempting to connect to the Deepgram WSS API directly I’ve received the correct error - 401 Unauthorised and the connection was denied. The SDK does not handle the 401 Unauthorised event properly perhaps because another error is thrown in the middle of the whole process. The error is the same as reported in the beginning of this issue.

Environment:

Steps to Reproduce:

  1. Get yourself an expired API key for Deepgram

  2. Clone the aforementioned repo and put your API key

  3. Run the server from the sample app

  4. Connect to your server at localhost (via a client or a tool)

  5. This will trigger the Node server implementation to try to connect to the Deepgram API

  6. An error is thrown unexpectedly from inside the SDK:

    node:events:505
    throw err; // Unhandled 'error' event
     ^
    
    Error [ERR_UNHANDLED_ERROR]
    // omitted for brevity
  7. (OPTIONAL) If you have correctly attached the LiveTranscriptionEvent.Close listener (not inside of the event handler for LiveTranscriptionEvents.Open but on the main level) you will also receive the event handled by the SDK that correctly understood the connection was closed:

{
   "type":"close",
   "isTrusted":false,
   "_yaeti":true,
   "code":1006,
   "reason":"connection failed",
   "wasClean":false,
   "target":{
      "_listeners":{

      },
      "_url":"wss://api.deepgram.com/v1/listen?smart_format=true&model=nova-2&language=fr&interim_results=true",
      "_readyState":3,
      "_extensions":"",
      "_bufferedAmount":0,
      "_binaryType":"arraybuffer",
      "_client":{
         "_events":{

         },
         "_eventsCount":0,
         "config":{
            "maxReceivedFrameSize":1048576,
            "maxReceivedMessageSize":8388608,
            "fragmentOutgoingMessages":true,
            "fragmentationThreshold":16384,
            "webSocketVersion":13,
            "assembleFragments":true,
            "disableNagleAlgorithm":true,
            "closeTimeout":5000,
            "tlsOptions":{

            }
         },
         "_req":null,
         "protocols":[
....
         ],
         "url":{
            "protocol":"wss:",
            "slashes":true,
            "auth":null,
            "host":"api.deepgram.com",
            "port":"443",
            "hostname":"api.deepgram.com",
            "hash":null,
            "search":"?smart_format=true&model=nova-2&language=fr&interim_results=true",
            "query":"smart_format=true&model=nova-2&language=fr&interim_results=true",
            "pathname":"/v1/listen",
            "path":"/v1/listen?smart_format=true&model=nova-2&language=fr&interim_results=true",
            "href":"wss://api.deepgram.com/v1/listen?smart_format=true&model=nova-2&language=fr&interim_results=true"
         },
         "secure":true,
         "base64nonce":"vKmLdRFpuL3GyGm4p9dHJw=="
      }
   },
   "cancelable":true
}

Further info:

When connecting via Postman directly to the WSS API - wss://api.deepgram.com/v1/listen?smart_format=true&model=nova-2&language=fr&interim_results=true and using the expired token in the Authorization header I receive the expected error - 401 Unauthorised.

Expected behaviour:

  1. The Deepgram JS SDK doesn't throw an error - proper error handling is done in the SDK
  2. The LiveTranscriptionEvent.Error event is called.

I hope this helps improving the SDK or someone stuck like me in debugging the error.

Thank you for the well-thought and architected SDK.

Originally posted by @AntonDobrev in https://github.com/deepgram/deepgram-js-sdk/issues/223#issuecomment-2139422234

CryptAlchemy commented 2 weeks ago

+1 for this

lukeocodes commented 2 weeks ago

In the websocket library we're using, if an error is thrown during connection, it is caught internally and throws rather than emitting an error. The thrown error also omits a ton of context.

I have a PR up (which I will be working to release THIS week) that will swap us over to native websocket clients (where available) and ws where in a node (or other supported) environment.

So, hopefully i'll be able to resolve this for you this week.