Skip to content

The open alternative to Salesforce, designed for AI.

Stop reporting suspended-workspace refusals to Sentry (#27361)

Charles Bochete6594729e1c2
diff --git a/packages/twenty-server/src/engine/core-modules/auth/auth.exception.ts b/packages/twenty-server/src/engine/core-modules/auth/auth.exception.ts
index 9e3550fb756..bcb27c6f65f 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/auth.exception.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/auth.exception.ts
@@ -96,6 +96,47 @@ const getAuthExceptionUserFriendlyMessage = (
}
};
+const getAuthExceptionStatusCode = (
+ code: keyof typeof AuthExceptionCode,
+): number => {
+ switch (code) {
+ case AuthExceptionCode.CLIENT_NOT_FOUND:
+ return 404;
+ case AuthExceptionCode.INVALID_INPUT:
+ return 400;
+ case AuthExceptionCode.FORBIDDEN_EXCEPTION:
+ case AuthExceptionCode.WORKSPACE_SUSPENDED:
+ case AuthExceptionCode.INSUFFICIENT_SCOPES:
+ case AuthExceptionCode.OAUTH_ACCESS_DENIED:
+ case AuthExceptionCode.SSO_AUTH_FAILED:
+ case AuthExceptionCode.USE_SSO_AUTH:
+ case AuthExceptionCode.SIGNUP_DISABLED:
+ case AuthExceptionCode.GOOGLE_API_AUTH_DISABLED:
+ case AuthExceptionCode.MICROSOFT_API_AUTH_DISABLED:
+ case AuthExceptionCode.MISSING_ENVIRONMENT_VARIABLE:
+ case AuthExceptionCode.EMAIL_NOT_VERIFIED:
+ case AuthExceptionCode.INVALID_JWT_TOKEN_TYPE:
+ case AuthExceptionCode.USER_ALREADY_EXISTS:
+ case AuthExceptionCode.ENTERPRISE_VALIDITY_TOKEN_NOT_VALID:
+ return 403;
+ case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_PROVISION_REQUIRED:
+ case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_VERIFICATION_REQUIRED:
+ case AuthExceptionCode.INVALID_DATA:
+ case AuthExceptionCode.UNAUTHENTICATED:
+ case AuthExceptionCode.APPLICATION_REFRESH_TOKEN_INVALID_OR_EXPIRED:
+ case AuthExceptionCode.USER_NOT_FOUND:
+ case AuthExceptionCode.WORKSPACE_NOT_FOUND:
+ case AuthExceptionCode.APPLICATION_NOT_FOUND:
+ return 401;
+ case AuthExceptionCode.INTERNAL_SERVER_ERROR:
+ case AuthExceptionCode.USER_WORKSPACE_NOT_FOUND:
+ return 500;
+ default: {
+ assertUnreachable(code);
+ }
+ }
+};
+
export class AuthException extends CustomException<
keyof typeof AuthExceptionCode
> {
@@ -107,6 +148,7 @@ export class AuthException extends CustomException<
super(message, code, {
userFriendlyMessage:
userFriendlyMessage ?? getAuthExceptionUserFriendlyMessage(code),
+ statusCode: getAuthExceptionStatusCode(code),
});
}
}
diff --git a/packages/twenty-server/src/engine/core-modules/auth/strategies/jwt.auth.strategy.spec.ts b/packages/twenty-server/src/engine/core-modules/auth/strategies/jwt.auth.strategy.spec.ts
index 93b7c7ddef7..b388b8f369f 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/strategies/jwt.auth.strategy.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/strategies/jwt.auth.strategy.spec.ts
@@ -254,7 +254,7 @@ describe('JwtAuthStrategy', () => {
await expect(strategy.validate(payload as JwtPayload)).rejects.toThrow(
new AuthException(
'User or user workspace not found',
- expect.any(String),
+ AuthExceptionCode.USER_NOT_FOUND,
{
userFriendlyMessage: msg`User does not have access to this workspace`,
},
@@ -290,7 +290,7 @@ describe('JwtAuthStrategy', () => {
await expect(strategy.validate(payload as JwtPayload)).rejects.toThrow(
new AuthException(
'User or user workspace not found',
- expect.any(String),
+ AuthExceptionCode.USER_NOT_FOUND,
{
userFriendlyMessage: msg`User does not have access to this workspace`,
},
@@ -399,7 +399,7 @@ describe('JwtAuthStrategy', () => {
await expect(strategy.validate(payload as JwtPayload)).rejects.toThrow(
new AuthException(
'User or user workspace not found',
- expect.any(String),
+ AuthExceptionCode.USER_NOT_FOUND,
{
userFriendlyMessage: msg`User does not have access to this workspace`,
},
@@ -501,9 +501,13 @@ describe('JwtAuthStrategy', () => {
strategy = createStrategy();
await expect(strategy.validate(payload as JwtPayload)).rejects.toThrow(
- new AuthException('Application not found', expect.any(String), {
- userFriendlyMessage: msg`Application not found.`,
- }),
+ new AuthException(
+ 'Application not found',
+ AuthExceptionCode.APPLICATION_NOT_FOUND,
+ {
+ userFriendlyMessage: msg`Application not found.`,
+ },
+ ),
);
try {
diff --git a/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-exception-rest-status.util.ts b/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-exception-rest-status.util.ts
deleted file mode 100644
index e8a1a0a20a4..00000000000
--- a/packages/twenty-server/src/engine/core-modules/auth/utils/get-auth-exception-rest-status.util.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { assertUnreachable } from 'twenty-shared/utils';
-
-import {
- type AuthException,
- AuthExceptionCode,
-} from 'src/engine/core-modules/auth/auth.exception';
-
-export const getAuthExceptionRestStatus = (exception: AuthException) => {
- switch (exception.code) {
- case AuthExceptionCode.CLIENT_NOT_FOUND:
- return 404;
- case AuthExceptionCode.INVALID_INPUT:
- return 400;
- case AuthExceptionCode.FORBIDDEN_EXCEPTION:
- case AuthExceptionCode.WORKSPACE_SUSPENDED:
- case AuthExceptionCode.INSUFFICIENT_SCOPES:
- case AuthExceptionCode.OAUTH_ACCESS_DENIED:
- case AuthExceptionCode.SSO_AUTH_FAILED:
- case AuthExceptionCode.USE_SSO_AUTH:
- case AuthExceptionCode.SIGNUP_DISABLED:
- case AuthExceptionCode.GOOGLE_API_AUTH_DISABLED:
- case AuthExceptionCode.MICROSOFT_API_AUTH_DISABLED:
- case AuthExceptionCode.MISSING_ENVIRONMENT_VARIABLE:
- case AuthExceptionCode.EMAIL_NOT_VERIFIED:
- case AuthExceptionCode.INVALID_JWT_TOKEN_TYPE:
- case AuthExceptionCode.USER_ALREADY_EXISTS:
- case AuthExceptionCode.ENTERPRISE_VALIDITY_TOKEN_NOT_VALID:
- return 403;
- case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_PROVISION_REQUIRED:
- case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_VERIFICATION_REQUIRED:
- case AuthExceptionCode.INVALID_DATA:
- case AuthExceptionCode.UNAUTHENTICATED:
- case AuthExceptionCode.APPLICATION_REFRESH_TOKEN_INVALID_OR_EXPIRED:
- case AuthExceptionCode.USER_NOT_FOUND:
- case AuthExceptionCode.WORKSPACE_NOT_FOUND:
- case AuthExceptionCode.APPLICATION_NOT_FOUND:
- return 401;
- case AuthExceptionCode.INTERNAL_SERVER_ERROR:
- case AuthExceptionCode.USER_WORKSPACE_NOT_FOUND:
- return 500;
- default: {
- assertUnreachable(exception.code);
- }
- }
-};
diff --git a/packages/twenty-server/src/engine/middlewares/middleware.service.ts b/packages/twenty-server/src/engine/middlewares/middleware.service.ts
index 415ade0ed11..1cf7b4c91c3 100644
--- a/packages/twenty-server/src/engine/middlewares/middleware.service.ts
+++ b/packages/twenty-server/src/engine/middlewares/middleware.service.ts
@@ -10,7 +10,6 @@ import {
} from 'src/engine/core-modules/auth/auth.exception';
import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
import { AccessTokenService } from 'src/engine/core-modules/auth/token/services/access-token.service';
-import { getAuthExceptionRestStatus } from 'src/engine/core-modules/auth/utils/get-auth-exception-rest-status.util';
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service';
import { ErrorCode } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';
@@ -209,7 +208,7 @@ export class MiddlewareService {
}
if (error instanceof AuthException) {
- return getAuthExceptionRestStatus(error);
+ return error.statusCode ?? 500;
}
return 500;
diff --git a/packages/twenty-server/src/engine/utils/__tests__/global-exception-handler.util.spec.ts b/packages/twenty-server/src/engine/utils/__tests__/global-exception-handler.util.spec.ts
index 3e07571a634..647c398b6fc 100644
--- a/packages/twenty-server/src/engine/utils/__tests__/global-exception-handler.util.spec.ts
+++ b/packages/twenty-server/src/engine/utils/__tests__/global-exception-handler.util.spec.ts
@@ -1,10 +1,17 @@
import { RetryableLogicFunctionError } from 'twenty-shared/logic-function';
+import {
+ AuthException,
+ AuthExceptionCode,
+} from 'src/engine/core-modules/auth/auth.exception';
import {
ThrottlerException,
ThrottlerExceptionCode,
} from 'src/engine/core-modules/throttler/throttler.exception';
-import { shouldCaptureException } from 'src/engine/utils/global-exception-handler.util';
+import {
+ handleException,
+ shouldCaptureException,
+} from 'src/engine/utils/global-exception-handler.util';
describe('shouldCaptureException', () => {
it('does not capture an explicitly retryable logic function error', () => {
@@ -28,7 +35,48 @@ describe('shouldCaptureException', () => {
).toBe(false);
});
+ it('does not capture a suspended workspace auth exception', () => {
+ expect(
+ shouldCaptureException(
+ new AuthException(
+ 'Workspace is suspended',
+ AuthExceptionCode.WORKSPACE_SUSPENDED,
+ ),
+ ),
+ ).toBe(false);
+ });
+
+ it('continues to capture an internal auth exception', () => {
+ expect(
+ shouldCaptureException(
+ new AuthException(
+ 'Unexpected auth failure',
+ AuthExceptionCode.INTERNAL_SERVER_ERROR,
+ ),
+ ),
+ ).toBe(true);
+ });
+
it('continues to capture an unexpected error', () => {
expect(shouldCaptureException(new Error('Unexpected failure'))).toBe(true);
});
});
+
+describe('handleException', () => {
+ it.each([
+ { code: AuthExceptionCode.WORKSPACE_SUSPENDED, isCaptured: false },
+ { code: AuthExceptionCode.INTERNAL_SERVER_ERROR, isCaptured: true },
+ ])(
+ 'should capture a $code auth exception: $isCaptured',
+ ({ code, isCaptured }) => {
+ const captureExceptions = jest.fn();
+
+ handleException({
+ exception: new AuthException('Auth failure', code),
+ exceptionHandlerService: { captureExceptions },
+ });
+
+ expect(captureExceptions).toHaveBeenCalledTimes(isCaptured ? 1 : 0);
+ },
+ );
+});
diff --git a/packages/twenty-server/src/engine/utils/global-exception-handler.util.ts b/packages/twenty-server/src/engine/utils/global-exception-handler.util.ts
index c0087f65717..17fb572980a 100644
--- a/packages/twenty-server/src/engine/utils/global-exception-handler.util.ts
+++ b/packages/twenty-server/src/engine/utils/global-exception-handler.util.ts
@@ -114,7 +114,7 @@ export const handleException = <
shouldBeCapturedBySentry = true,
}: {
exception: T;
- exceptionHandlerService: ExceptionHandlerService;
+ exceptionHandlerService: Pick<ExceptionHandlerService, 'captureExceptions'>;
user?: ExceptionHandlerUser;
workspace?: ExceptionHandlerWorkspace;
statusCode?: number;