Skip to content

Commit

Permalink
[Slack connector deletion] Skip app uninstall if nango conn. unknown (#…
Browse files Browse the repository at this point in the history
…3419)

* [Slack connector deletion] Skip app uninstall if nango connection unknown

* update from review

* cleaning
  • Loading branch information
philipperolet authored Jan 25, 2024
1 parent 7dcb1bf commit 7d75d7c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
16 changes: 12 additions & 4 deletions connectors/src/connectors/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 { ExternalOauthTokenError, NangoError } from "@connectors/lib/error";
import { Connector, sequelize_conn } from "@connectors/lib/models";
import {
SlackChannel,
Expand Down Expand Up @@ -248,9 +248,8 @@ export async function uninstallSlack(nangoConnectionId: string) {
throw new Error("SLACK_CLIENT_SECRET is not defined");
}

const slackAccessToken = await getSlackAccessToken(nangoConnectionId);

try {
const slackAccessToken = await getSlackAccessToken(nangoConnectionId);
const slackClient = await getSlackClient(slackAccessToken);
await slackClient.auth.test();
const deleteRes = await slackClient.apps.uninstall({
Expand All @@ -265,7 +264,16 @@ export async function uninstallSlack(nangoConnectionId: string) {
);
}
} catch (e) {
if (e instanceof ExternalOauthTokenError) {
if (e instanceof NangoError && e.type === "unknown_connection") {
logger.info(
{
nangoConnectionId,
error: `Nango error: unknown connection: ${e.message}`,
},
"Unknown nango connection, skipping uninstallation of the Slack app"
);
return new Ok(undefined);
} else if (e instanceof ExternalOauthTokenError) {
logger.info(
{
nangoConnectionId,
Expand Down
13 changes: 13 additions & 0 deletions connectors/src/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export class ExternalOauthTokenError extends Error {
}
}

export const NANGO_ERROR_TYPES = ["unknown_connection"];
export type NangoErrorType = (typeof NANGO_ERROR_TYPES)[number];
export class NangoError extends Error {
readonly type: NangoErrorType;

constructor(type: NangoErrorType, readonly innerError?: Error) {
super(innerError?.message);
this.name = "NangoError";
this.type = type;
this.innerError = innerError;
}
}

export function isNotFoundError(err: unknown): err is NotFoundError {
return err instanceof HTTPError && err.statusCode === 404;
}
15 changes: 11 additions & 4 deletions connectors/src/lib/nango_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { Nango } from "@nangohq/node";
import axios from "axios";

import type { WorkflowError } from "@connectors/lib/error";
import { ExternalOauthTokenError } from "@connectors/lib/error";

import type { Result } from "./result";
import { Err, Ok } from "./result";
import {
ExternalOauthTokenError,
NANGO_ERROR_TYPES,
NangoError,
} from "@connectors/lib/error";
import type { Result } from "@connectors/lib/result";
import { Err, Ok } from "@connectors/lib/result";

const { NANGO_SECRET_KEY } = process.env;

Expand Down Expand Up @@ -35,6 +38,10 @@ class CustomNango extends Nango {
) {
throw new ExternalOauthTokenError();
}
const errorType = e.response.data.type;
if (NANGO_ERROR_TYPES.includes(errorType)) {
throw new NangoError(errorType, e);
}
}
}
if (e.status === 520 && e.code === "ERR_BAD_RESPONSE") {
Expand Down

0 comments on commit 7d75d7c

Please sign in to comment.