Skip to content

Commit

Permalink
confluence + rate limit as 429
Browse files Browse the repository at this point in the history
  • Loading branch information
spolu committed Jan 2, 2025
1 parent 743ca18 commit 0313b5b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
1 change: 0 additions & 1 deletion connectors/src/api/get_connector_permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { Request, Response } from "express";

import { getConnectorManager } from "@connectors/connectors";
import { augmentContentNodesWithParentIds } from "@connectors/lib/api/content_nodes";
import { ProviderWorkflowError } from "@connectors/lib/error";
import logger from "@connectors/logger/logger";
import { apiError, withLogging } from "@connectors/logger/withlogging";
import { ConnectorResource } from "@connectors/resources/connector_resource";
Expand Down
79 changes: 54 additions & 25 deletions connectors/src/connectors/confluence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ContentNodesViewType,
Result,
} from "@dust-tt/types";
import { cacheWithRedis, Err, Ok } from "@dust-tt/types";
import { cacheWithRedis, ConfluenceClientError, Err, Ok } from "@dust-tt/types";

import {
getConfluenceAccessToken,
Expand Down Expand Up @@ -38,13 +38,15 @@ import {
} from "@connectors/connectors/confluence/temporal/client";
import type {
CreateConnectorErrorCode,
RetrievePermissionsErrorCode,
UpdateConnectorErrorCode,
} from "@connectors/connectors/interface";
import {
BaseConnectorManager,
ConnectorManagerError,
} from "@connectors/connectors/interface";
import { concurrentExecutor } from "@connectors/lib/async_utils";
import { ExternalOAuthTokenError } from "@connectors/lib/error";
import {
ConfluenceConfiguration,
ConfluencePage,
Expand Down Expand Up @@ -265,11 +267,15 @@ export class ConfluenceConnectorManager extends BaseConnectorManager<null> {
}: {
parentInternalId: string | null;
filterPermission: ConnectorPermission | null;
}): Promise<Result<ContentNode[], Error>> {
}): Promise<
Result<ContentNode[], ConnectorManagerError<RetrievePermissionsErrorCode>>
> {
const connector = await ConnectorResource.fetchById(this.connectorId);
if (!connector) {
logger.error({ connectorId: this.connectorId }, "Connector not found");
return new Err(new Error("Connector not found"));
return new Err(
new ConnectorManagerError("CONNECTOR_NOT_FOUND", "Connector not found")
);
}

const confluenceConfig = await ConfluenceConfiguration.findOne({
Expand All @@ -282,32 +288,55 @@ export class ConfluenceConnectorManager extends BaseConnectorManager<null> {
{ connectorId: this.connectorId },
"Confluence configuration not found"
);
return new Err(new Error("Confluence configuration not found"));
throw new Error("Confluence configuration not found");
}

// When the filter permission is set to 'read', the full hierarchy of spaces
// and pages that Dust can access is displayed to the user.
if (filterPermission === "read") {
const data = await retrieveHierarchyForParent(
connector,
confluenceConfig,
parentInternalId
);

if (data.isErr()) {
return new Err(data.error);
}
try {
// When the filter permission is set to 'read', the full hierarchy of spaces
// and pages that Dust can access is displayed to the user.
if (filterPermission === "read") {
const data = await retrieveHierarchyForParent(
connector,
confluenceConfig,
parentInternalId
);
if (data.isErr()) {
throw data.error;
}

return new Ok(data.value);
} else {
// If the permission is not set to 'read', users are limited to selecting only
// spaces for synchronization with Dust.
const allSpacesRes = await retrieveAvailableSpaces(
connector,
confluenceConfig
);
return new Ok(data.value);
} else {
// If the permission is not set to 'read', users are limited to selecting only
// spaces for synchronization with Dust.
const allSpacesRes = await retrieveAvailableSpaces(
connector,
confluenceConfig
);
if (allSpacesRes.isErr()) {
throw allSpacesRes.error;
}

return allSpacesRes;
return allSpacesRes;
}
} catch (e) {
if (e instanceof ExternalOAuthTokenError) {
return new Err(
new ConnectorManagerError(
"EXTERNAL_OAUTH_TOKEN_ERROR",
"Confluence authorization error, please re-authorize."
)
);
}
if (e instanceof ConfluenceClientError && e.status === 429) {
return new Err(
new ConnectorManagerError(
"RATE_LIMIT_ERROR",
`Confluence rate limit error when retrieving content nodes.`
)
);
}
// Unanhdled error, throwing to get a 500.
throw e;
}
}

Expand Down

0 comments on commit 0313b5b

Please sign in to comment.