diff --git a/connectors/src/connectors/slack/index.ts b/connectors/src/connectors/slack/index.ts index ddb78dee3e4e..60e42b0a469c 100644 --- a/connectors/src/connectors/slack/index.ts +++ b/connectors/src/connectors/slack/index.ts @@ -1,10 +1,5 @@ import { ModelId } from "@dust-tt/types"; -import { - CodedError, - ErrorCode, - WebAPIPlatformError, - WebClient, -} from "@slack/web-api"; +import { WebClient } from "@slack/web-api"; import PQueue from "p-queue"; import { ConnectorPermissionRetriever } from "@connectors/connectors/interface"; @@ -15,6 +10,7 @@ import { getSlackClient, } from "@connectors/connectors/slack/lib/slack_client"; import { launchSlackSyncWorkflow } from "@connectors/connectors/slack/temporal/client.js"; +import { ExternalOauthTokenError } from "@connectors/lib/error"; import { Connector, sequelize_conn } from "@connectors/lib/models"; import { SlackChannel, @@ -245,8 +241,9 @@ export async function uninstallSlack(nangoConnectionId: string) { } const slackAccessToken = await getSlackAccessToken(nangoConnectionId); - const slackClient = await getSlackClient(slackAccessToken); + try { + const slackClient = await getSlackClient(slackAccessToken); await slackClient.auth.test(); const deleteRes = await slackClient.apps.uninstall({ client_id: SLACK_CLIENT_ID, @@ -260,25 +257,14 @@ export async function uninstallSlack(nangoConnectionId: string) { ); } } catch (e) { - const slackError = e as CodedError; - let shouldThrow = true; - - if (slackError.code === ErrorCode.PlatformError) { - const platformError = e as WebAPIPlatformError; - if ( - ["account_inactive", "invalid_auth"].includes(platformError.data.error) - ) { - shouldThrow = false; - logger.info( - { - nangoConnectionId, - }, - `Slack auth is invalid, skipping uninstallation of the Slack app` - ); - } - } - - if (shouldThrow) { + if (e instanceof ExternalOauthTokenError) { + logger.info( + { + nangoConnectionId, + }, + `Slack auth is invalid, skipping uninstallation of the Slack app` + ); + } else { throw e; } } diff --git a/connectors/src/connectors/slack/lib/slack_client.ts b/connectors/src/connectors/slack/lib/slack_client.ts index 928f0ba4fc30..e9dd80c6bf1c 100644 --- a/connectors/src/connectors/slack/lib/slack_client.ts +++ b/connectors/src/connectors/slack/lib/slack_client.ts @@ -3,10 +3,11 @@ import { CodedError, ErrorCode, WebAPIHTTPError, + WebAPIPlatformError, WebClient, } from "@slack/web-api"; -import { WorkflowError } from "@connectors/lib/error"; +import { ExternalOauthTokenError, WorkflowError } from "@connectors/lib/error"; import { Connector } from "@connectors/lib/models"; import { getAccessTokenFromNango } from "@connectors/lib/nango_helpers"; const { NANGO_SLACK_CONNECTOR_ID } = process.env; @@ -80,6 +81,16 @@ export async function getSlackClient( throw workflowError; } } + if (slackError.code === ErrorCode.PlatformError) { + const platformError = e as WebAPIPlatformError; + if ( + ["account_inactive", "invalid_auth"].includes( + platformError.data.error + ) + ) { + throw new ExternalOauthTokenError(); + } + } throw e; } },