Skip to content

Commit

Permalink
Handle errors from Cosmos better
Browse files Browse the repository at this point in the history
Co-authored-by: Fritz Madden <[email protected]>
Co-authored-by: Arthur Morrow <[email protected]>
  • Loading branch information
3 people committed Oct 16, 2024
1 parent a6d7077 commit ee97959
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { CamsSession } from '../../../../../common/src/cams/session';
import { UserSessionCacheRepository } from './user-session-cache.repository';
import { UnauthorizedError } from '../../common-errors/unauthorized-error';
import { CamsJwtClaims } from '../../../../../common/src/cams/jwt';
import { getCamsError } from '../../common-errors/error-utilities';
import { isConflictError } from '../../use-cases/user-session/user-session';

const MODULE_NAME: string = 'COSMOS_DB_REPOSITORY_USER_SESSION_CACHE';
const CONTAINER_NAME: string = 'user-session-cache';
Expand Down Expand Up @@ -38,7 +40,14 @@ export class UserSessionCacheCosmosDbRepository implements UserSessionCacheRepos
],
};

const cached = await this.repo.query(context, querySpec);
let cached;
try {
cached = await this.repo.query(context, querySpec);
} catch (error) {
if (isConflictError(error)) throw error;
throw getCamsError(error, MODULE_NAME);
}

if (cached && cached.length === 1) {
return toCamsSession(cached[0]);
} else {
Expand Down
15 changes: 9 additions & 6 deletions backend/functions/lib/use-cases/user-session/user-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ export class UserSessionUseCase {
await sessionCacheRepository.put(context, session);

return session;
} catch (originalError) {
if (isConflictError(originalError)) {
} catch (error) {
const isConflict = error.originalError
? isConflictError(error.originalError)
: isConflictError(error);
if (isConflict) {
return await sessionCacheRepository.get(context, token);
}

throw isCamsError(originalError)
? originalError
throw isCamsError(error)
? error
: new UnauthorizedError(MODULE_NAME, {
message: originalError.message,
originalError,
message: error.message,
originalError: error,
});
}
}
Expand Down

0 comments on commit ee97959

Please sign in to comment.