Skip to content

feat(socket-mode): replace error interfaces with proper Error subclasses#2596

Merged
WilliamBergamin merged 2 commits into
v8from
imporve-error-handling-in-socket-oauth
May 14, 2026
Merged

feat(socket-mode): replace error interfaces with proper Error subclasses#2596
WilliamBergamin merged 2 commits into
v8from
imporve-error-handling-in-socket-oauth

Conversation

@WilliamBergamin
Copy link
Copy Markdown
Contributor

@WilliamBergamin WilliamBergamin commented May 14, 2026

Summary

This PR replaces the error factory-function pattern in @slack/socket-mode with proper Error subclasses, bringing it in line with the same change already applied to @slack/web-api in #2593.

What changed

The old approach used interfaces (e.g. SMWebsocketError, SMPlatformError) and factory functions (e.g. websocketErrorWithOriginal(), platformErrorFromEvent()) that mutated plain Error objects by attaching a code property. This made instanceof checks unreliable.

The new approach defines real ES6 class subclasses of Error:

  • SMWebsocketError
  • SMPlatformError
  • SMNoReplyReceivedError
  • SMSendWhileDisconnectedError
  • SMSendWhileNotReadyError

How users can take advantage of these changes

Before (fragile string-based check):

 import { ErrorCode } from '@slack/socket-mode';

 client.on('error', (err) => {
   if (err.code === ErrorCode.WebsocketError) {
     console.log('WebSocket issue:', err.original?.message);
   }
 });

After (type-safe instanceof check):

 import { SMWebsocketError, SMSendWhileDisconnectedError } from '@slack/socket-mode';

 client.on('error', (err) => {
   if (err instanceof SMWebsocketError) {
     // TypeScript knows `err.original` and `err.cause` exist
     console.log('WebSocket issue:', err.original.message);
   }
 });

Catching send errors:

 import { SMSendWhileDisconnectedError, SMSendWhileNotReadyError } from '@slack/socket-mode';

 try {
   await client.send({ type: 'message', text: 'hello' });
 } catch (err) {
   if (err instanceof SMSendWhileDisconnectedError) {
     // reconnect logic
   } else if (err instanceof SMSendWhileNotReadyError) {
     // wait for 'connected' event
   }
 }

Checking connection errors during start():

 import { WebAPIHTTPError, WebAPIPlatformError, WebAPIRequestError } from '@slack/web-api';

 try {
   await client.start();
 } catch (err) {
   if (err instanceof WebAPIPlatformError) {
     console.log('Platform said:', err.data.error);
   } else if (err instanceof WebAPIHTTPError) {
     console.log('HTTP status:', err.statusCode);
   } else if (err instanceof WebAPIRequestError) {
     console.log('Network problem:', err.original.message);
   }
 }

Additionally, the SocketModeClient now uses proper instanceof checks when classifying errors from apps.connections.open to decide whether to auto-reconnect.

Requirements

@WilliamBergamin WilliamBergamin requested a review from a team as a code owner May 14, 2026 19:46
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 14, 2026

⚠️ No Changeset found

Latest commit: 826d039

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@WilliamBergamin WilliamBergamin self-assigned this May 14, 2026
@WilliamBergamin WilliamBergamin added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented pkg:socket-mode applies to `@slack/socket-mode` labels May 14, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (v8@187594b). Learn more about missing BASE report.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@          Coverage Diff          @@
##             v8    #2596   +/-   ##
=====================================
  Coverage      ?   87.39%           
=====================================
  Files         ?       62           
  Lines         ?    10083           
  Branches      ?      425           
=====================================
  Hits          ?     8812           
  Misses        ?     1248           
  Partials      ?       23           
Flag Coverage Δ
cli-hooks 87.39% <100.00%> (?)
cli-test 87.39% <100.00%> (?)
logger 87.39% <100.00%> (?)
oauth 87.39% <100.00%> (?)
socket-mode 87.39% <100.00%> (?)
web-api 87.39% <100.00%> (?)
webhook 87.39% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WilliamBergamin WilliamBergamin merged commit 348b453 into v8 May 14, 2026
10 checks passed
@WilliamBergamin WilliamBergamin deleted the imporve-error-handling-in-socket-oauth branch May 14, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented pkg:socket-mode applies to `@slack/socket-mode`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant