Skip to content

fix: handle network errors during login to prevent TypeError crash#1261

Open
mufeezhanif wants to merge 1 commit intoRocketChat:developfrom
mufeezhanif:fix/handle-network-error-login
Open

fix: handle network errors during login to prevent TypeError crash#1261
mufeezhanif wants to merge 1 commit intoRocketChat:developfrom
mufeezhanif:fix/handle-network-error-login

Conversation

@mufeezhanif
Copy link
Copy Markdown

Summary

Fixes #1210 — login crashes with TypeError: Cannot read properties of undefined (reading 'error') when a network error occurs during authentication.

Root Cause

The login method in EmbeddedChatApi.ts catches errors but only returns a structured response for 401 errors. For all other errors (network failures, DNS errors, etc.), it console.error()s and implicitly returns undefined. The calling code in useRCAuth.js then tries to access res.error, which throws a TypeError.

Changes

packages/api/src/EmbeddedChatApi.ts

  • Return { error: error.message } (or { error: 'unknown-error' }) for all non-401 errors instead of implicitly returning undefined

packages/react/src/hooks/useRCAuth.js

  • Added a null guard on the login response as a defensive check
  • Shows a user-friendly toast message ("Unable to connect to server...") if the response is missing

Testing

  • Verified the fix handles the case described in the issue (network error → graceful toast instead of crash)
  • The existing catch block in handleLogin still handles truly unexpected errors

The login method in EmbeddedChatApi.ts returned undefined when a
non-401 error occurred (e.g. network failure). This caused a
TypeError in useRCAuth.js when accessing res.error on the undefined
response, crashing the login flow silently.

Changes:
- EmbeddedChatApi.ts: return an error object for all caught errors
  instead of implicitly returning undefined
- useRCAuth.js: add a null guard on the login response as a safety
  net, showing a toast message if the response is missing

Fixes RocketChat#1210
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: Uncaught TypeError in useRCAuth on Network Error during login

1 participant