Skip to content

Commit

Permalink
Centralize account_inactive error in Slack Client (#3069)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Jan 8, 2024
1 parent 4eefda8 commit eff3c2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
38 changes: 12 additions & 26 deletions connectors/src/connectors/slack/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
Expand Down
13 changes: 12 additions & 1 deletion connectors/src/connectors/slack/lib/slack_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
},
Expand Down

0 comments on commit eff3c2f

Please sign in to comment.