Skip to content

fix(api,react): propagate _doConnect errors and reset auth state on failure - #1485

Open
ashwanisingh011 wants to merge 1 commit into
RocketChat:developfrom
ashwanisingh011:fix/propagate-doconnect-errors
Open

ashwanisingh011 wants to merge 1 commit into
RocketChat:developfrom
ashwanisingh011:fix/propagate-doconnect-errors

Conversation

@ashwanisingh011

@ashwanisingh011 ashwanisingh011 commented Dec 3, 2026 •

Copy link
Copy Markdown

fix(api,react): propagate _doConnect errors instead of resolving silently

Summary

Previously, EmbeddedChatApi._doConnect() caught connection/login errors, called this.close() without await, and swallowed the error instead of rethrowing it. As a result, RCInstance.connect() resolved instead of rejecting—causing EmbeddedChat to falsely log "Connected to RocketChat ...", set isUserAuthenticated(true), and trigger authenticated REST fetches that failed with 401 (Unauthorized) and a TypeError in useFetchChatData.

Fixes: #1484

Changes

  • packages/api/src/EmbeddedChatApi.ts:
    • await this.close() before connecting and inside the catch (err) block.
    • Rethrow err from _doConnect() so RCInstance.connect() rejects on failure.
  • packages/react/src/views/EmbeddedChat.js:
    • In RCInstance.connect(...).catch(...), log the error and reset setIsUserAuthenticated(false) when still mounted for the same user, preventing stale/broken authenticated UI state on connection failure.

Acceptance Criteria fulfillment

  • _doConnect() awaits this.close() and rethrows connection/auth errors instead of resolving silently.
  • EmbeddedChat handles rejected RCInstance.connect() promises without transitioning into a broken authenticated state.
  • Verified Before/After behavior locally in Storybook against a running Rocket.Chat server.

Video/Screenshots

Before (develop) After (fix/propagate-doconnect-errors)
Screenshot 2026-12-05 at 1 32 09 AM Screenshot 2026-12-05 at 1 35 26 AM
_doConnect swallows the thrown error: EmbeddedChat.js:206 falsely logs Connected to RocketChat http://localhost:3000, shows Message #undefined, and triggers 401 (Unauthorized) errors (19 console errors). _doConnect awaits close() and rethrows: EmbeddedChat.js:218 logs Error: Simulated WebSocket failure cleanly, and UI stays in the unauthenticated JOIN state (1 console error, zero 401 cascade).

PR Test Details

  1. Ran a local Rocket.Chat server (http://localhost:3000, CORS enabled) and EmbeddedChat Storybook (http://localhost:6006, EmbeddedChat/Simple).
  2. Set a DevTools Sources breakpoint on await this.sdk.connection.connect(); in EmbeddedChatApi.ts and simulated a WebSocket failure:
    this.sdk.connection.connect = async () => { throw new Error("Simulated WebSocket failure"); }
  3. Before (develop): Console printed Connected to RocketChat http://localhost:3000 followed by 401 (Unauthorized) errors across room endpoints (rooms.info, permissions.listAll, channels.messages, etc.) while UI rendered Message #undefined.
  4. After (fix/propagate-doconnect-errors): Rebuilt @embeddedchat/api (yarn build), restarted Storybook, repeated the same simulation—Console logged only Error: Simulated WebSocket failure at EmbeddedChat.js:218, with no false "Connected" log, no 401 cascade, and UI remaining in the JOIN state.

Note: The PR will be ready for live testing at https://rocketchat.github.io/EmbeddedChat/pulls/pr-<pr_number> after approval. Contributors are requested to replace <pr_number> with the actual PR number.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

bug(api): _doConnect swallows connection/auth errors and marks failed sessions as authenticated

1 participant