Skip to content

Commit

Permalink
feat: propagate original error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederson committed Jan 16, 2025
1 parent 6e91418 commit baac9a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
12 changes: 5 additions & 7 deletions src/modules/user/user.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,24 +1275,24 @@ describe('User use cases', () => {
expect(result).toMatchObject({ avatar: newAvatarURL });
});

it('When deleting the old avatar fails then throw InternalServerErrorException', async () => {
it('When deleting the old avatar fails then throw', async () => {
jest
.spyOn(avatarService, 'deleteAvatar')
.mockRejectedValue(new Error('Delete failed'));

await expect(
userUseCases.upsertAvatar(user, newAvatarKey),
).rejects.toThrow(InternalServerErrorException);
).rejects.toThrow();
});

it('When updating the user avatar fails then throw InternalServerErrorException', async () => {
it('When updating the user avatar fails then throw', async () => {
jest
.spyOn(userRepository, 'updateById')
.mockRejectedValue(new Error('Update failed'));

await expect(
userUseCases.upsertAvatar(user, newAvatarKey),
).rejects.toThrow(InternalServerErrorException);
).rejects.toThrow();
});

it('When user has no avatar already, then previous avatar is not deleted', async () => {
Expand Down Expand Up @@ -1338,9 +1338,7 @@ describe('User use cases', () => {
.spyOn(avatarService, 'deleteAvatar')
.mockRejectedValue(new Error('Delete failed'));

await expect(userUseCases.deleteAvatar(user)).rejects.toThrow(
InternalServerErrorException,
);
await expect(userUseCases.deleteAvatar(user)).rejects.toThrow();
expect(userRepository.updateById).not.toHaveBeenCalled();
});
});
Expand Down
33 changes: 10 additions & 23 deletions src/modules/user/user.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,34 +1384,21 @@ export class UserUseCases {
avatarKey: string,
): Promise<Error | { avatar: string }> {
if (user.avatar) {
try {
await this.avatarService.deleteAvatar(user.avatar);
} catch (err) {
throw new InternalServerErrorException('Failed to delete old avatar');
}
}

try {
await this.userRepository.updateById(user.id, {
avatar: avatarKey,
});
const avatarUrl = await this.getAvatarUrl(avatarKey);
return { avatar: avatarUrl };
} catch (err) {
throw new InternalServerErrorException('Failed to add new avatar');
await this.avatarService.deleteAvatar(user.avatar);
}
await this.userRepository.updateById(user.id, {
avatar: avatarKey,
});
const avatarUrl = await this.getAvatarUrl(avatarKey);
return { avatar: avatarUrl };
}

async deleteAvatar(user: User): Promise<Error | void> {
if (user.avatar) {
try {
await this.avatarService.deleteAvatar(user.avatar);
await this.userRepository.updateById(user.id, {
avatar: null,
});
} catch (err) {
throw new InternalServerErrorException('Failed to delete avatar');
}
await this.avatarService.deleteAvatar(user.avatar);
await this.userRepository.updateById(user.id, {
avatar: null,
});
}
}

Expand Down

0 comments on commit baac9a0

Please sign in to comment.