add exception handling

This commit is contained in:
mirrerror
2023-11-13 10:11:44 +02:00
parent acbb6285d9
commit 3ba95647b4
10 changed files with 168 additions and 36 deletions

View File

@@ -5,6 +5,7 @@ import com.faf223.expensetrackerfaf.dto.UserDTO;
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.service.UserService;
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -32,7 +33,7 @@ public class UserController {
userService.updateUser(user);
return ResponseEntity.ok(userMapper.toDto(user));
} else {
return ResponseEntity.notFound().build();
throw new UserNotFoundException("The user has not been found");
}
}
@@ -43,9 +44,8 @@ public class UserController {
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
User user = userService.getUserByEmail(userDetails.getUsername());
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
else return ResponseEntity.notFound().build();
}
return ResponseEntity.notFound().build();
throw new UserNotFoundException("The user has not been found");
}
@GetMapping()