Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 7b58fea40f fix(auth): throw AuthException in getUserWorkspaceForUserOrThrow
https://sonarly.com/issue/15618?type=bug

When a user exchanges a login token but has no active userWorkspace record for the target workspace, `getUserWorkspaceForUserOrThrow` throws a plain `Error` instead of a typed `AuthException`. This causes a 500 Internal Server Error instead of a proper 401 authentication error, and surfaces as an untyped error in Sentry.

Fix: Changed `getUserWorkspaceForUserOrThrow` in `user-workspace.service.ts` to throw `AuthException` with `AuthExceptionCode.USER_WORKSPACE_NOT_FOUND` instead of a plain `Error`.

**Why:** The plain `Error('User workspace not found')` bypassed the typed exception handling pipeline. The GraphQL exception handler (`auth-graphql-api-exception-handler.util.ts`) already maps `USER_WORKSPACE_NOT_FOUND` to an `AuthenticationError` (401), but the plain `Error` was never routed through it — resulting in a 500 Internal Server Error in Sentry instead of a proper 401.

The `AuthException` and `AuthExceptionCode` imports were already present in the file (lines 14-17), so no new imports were needed. This matches the pattern used in 5+ other locations across the codebase for the same condition (e.g., `jwt.auth.strategy.ts:153`, `auth.service.ts:661`, `workspace-member-update-one.pre-query.hook.ts:67`).

Updated the corresponding test to assert on `AuthException` type instead of the error message string.
2026-03-17 15:41:18 +00:00
2 changed files with 5 additions and 2 deletions
@@ -784,7 +784,7 @@ describe('UserWorkspaceService', () => {
await expect(
service.getUserWorkspaceForUserOrThrow({ userId, workspaceId }),
).rejects.toThrow('User workspace not found');
).rejects.toThrow(AuthException);
});
});
@@ -372,7 +372,10 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspaceEntit
});
if (!isDefined(userWorkspace)) {
throw new Error('User workspace not found');
throw new AuthException(
'User workspace not found',
AuthExceptionCode.USER_WORKSPACE_NOT_FOUND,
);
}
return userWorkspace;