From 253ac2f0102dced21720f81735dac2a798e64659 Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Fri, 10 May 2024 10:29:22 +0200 Subject: [PATCH] exception handling better good --- Core/Services/UserService.cs | 2 +- api/Events/User/ClientWantsToUpdateProfile.cs | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/Core/Services/UserService.cs b/Core/Services/UserService.cs index ae4a898..1dfc155 100644 --- a/Core/Services/UserService.cs +++ b/Core/Services/UserService.cs @@ -39,7 +39,7 @@ public async Task GetUserByEmail(string email) public async Task 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)) { diff --git a/api/Events/User/ClientWantsToUpdateProfile.cs b/api/Events/User/ClientWantsToUpdateProfile.cs index 634450e..ac2b22d 100644 --- a/api/Events/User/ClientWantsToUpdateProfile.cs +++ b/api/Events/User/ClientWantsToUpdateProfile.cs @@ -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",