Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ivan-juni opened this issue Jun 3, 2024 · 4 comments · Fixed by #293

Comments

@Ivan-juni
Copy link

Ivan-juni commented Jun 3, 2024

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:

  • Deepgram JS SDK 3.3.3
  • Node.js v20.9.0

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
  1. (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 #223 (comment)

@CryptAlchemy
Copy link

+1 for this

@lukeocodes
Copy link
Contributor

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.

@CryptAlchemy
Copy link

Hello,

I'd like to please ask for an update on this bug.

Thanks!

@lukeocodes
Copy link
Contributor

latest version goes out today. you'll get a better exception, Error: Unexpected server response: 401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants