Skip to content

Commit

Permalink
[Microsoft] Move Application disabled error to oauth (#7734)
Browse files Browse the repository at this point in the history
Description
---
This kind of error should be handled in oauth.

Risks
---
na

Deploy
---
- core
- connectors
  • Loading branch information
philipperolet authored Sep 27, 2024
1 parent 3d62e0f commit 26f642a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
17 changes: 1 addition & 16 deletions connectors/src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// JS cannot give you any guarantee about the shape of an error you `catch`

import type {
APIError,
ConnectorProvider,
OAuthAPIError,
} from "@dust-tt/types";
import type { APIError, ConnectorProvider } from "@dust-tt/types";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function errorFromAny(e: any): Error {
Expand Down Expand Up @@ -76,17 +72,6 @@ export function isNotFoundError(err: unknown): err is NotFoundError {
return err instanceof HTTPError && err.statusCode === 404;
}

export function isMicrosoftApplicationDisabledError(
error: OAuthAPIError,
provider: string
): boolean {
return (
error.code === "provider_access_token_refresh_error" &&
provider === "microsoft" &&
/Application.*is disabled/.test(error.message)
);
}

// Error for invalid rows when upserting table
export class InvalidRowsRequestError extends Error {
constructor(message: string) {
Expand Down
8 changes: 2 additions & 6 deletions connectors/src/lib/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { getOAuthConnectionAccessToken } from "@dust-tt/types";
import type { LoggerInterface } from "@dust-tt/types/dist/shared/logger";

import { apiConfig } from "@connectors/lib/api/config";
import {
ExternalOAuthTokenError,
isMicrosoftApplicationDisabledError,
} from "@connectors/lib/error";
import { ExternalOAuthTokenError } from "@connectors/lib/error";

// Most connectors are built on the assumption that errors are thrown with special handling of
// selected errors such as ExternalOauthTokenError. This function is used to retrieve an OAuth
Expand Down Expand Up @@ -40,8 +37,7 @@ export async function getOAuthConnectionAccessTokenWithThrow({

if (
tokRes.error.code === "token_revoked_error" ||
tokRes.error.code === "connection_not_found" ||
isMicrosoftApplicationDisabledError(tokRes.error, provider)
tokRes.error.code === "connection_not_found"
) {
throw new ExternalOAuthTokenError(new Error(tokRes.error.message));
} else {
Expand Down
12 changes: 12 additions & 0 deletions core/src/oauth/providers/microsoft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use lazy_static::lazy_static;
use regex::Regex;
use serde_json::json;
use std::env;

Expand Down Expand Up @@ -143,6 +144,8 @@ impl Provider for MicrosoftConnectionProvider {
}

fn handle_provider_request_error(&self, error: ProviderHttpRequestError) -> ProviderError {
let app_disabled_regex = Regex::new(r"Application.*is disabled").unwrap();

match &error {
ProviderHttpRequestError::RequestFailed {
status, message, ..
Expand All @@ -158,6 +161,15 @@ impl Provider for MicrosoftConnectionProvider {
self.default_handle_provider_request_error(error)
}
}
ProviderHttpRequestError::RequestFailed {
status, message, ..
} if *status == 403 => {
if app_disabled_regex.is_match(message) {
ProviderError::TokenRevokedError
} else {
self.default_handle_provider_request_error(error)
}
}
_ => {
// Call the default implementation for other cases.
self.default_handle_provider_request_error(error)
Expand Down

0 comments on commit 26f642a

Please sign in to comment.