Skip to content

Commit

Permalink
exception handling better good
Browse files Browse the repository at this point in the history
  • Loading branch information
mariaruth1 committed May 10, 2024
1 parent 799db7a commit 253ac2f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Core/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<User> GetUserByEmail(string email)
public async Task<GetUserDto?> UpdateUser(UpdateUserDto updateUserDto, string email)
{
var userToUpdate = await userRepository.GetUserByEmail(email);
if (userToUpdate == null) return null;
if (userToUpdate == null) throw new NotFoundException("User not found");

if (updateUserDto.Username != null && !updateUserDto.Username.Equals(string.Empty))
{
Expand Down
11 changes: 3 additions & 8 deletions api/Events/User/ClientWantsToUpdateProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,16 @@ public class ClientWantsToUpdateProfile (UserService userService, JwtService jwt
public override async Task Handle(ClientWantsToUpdateUserDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt);
var getUserDto = await userService.UpdateUser(dto.UpdateUserDto, email);

if (getUserDto != null)
try
{
var getUserDto = await userService.UpdateUser(dto.UpdateUserDto, email);
socket.SendDto(new ServerConfirmsUpdate
{
GetUserDto = getUserDto
});
}
else
} catch (Exception e) when (e is not NotFoundException)
{
var user = await userService.GetUserByEmail(email);

if (user == null) throw new NotFoundException("User not found");

socket.SendDto(new ServerRejectsUpdate
{
ErrorMessage = "Update failed",
Expand Down

0 comments on commit 253ac2f

Please sign in to comment.