I have the following problem.
I need to perform a process of updating the user and his group, and to make the change I search the user, validating if it exists and soon after, I look for the group checking if it exists, if any mistake happens I upload one new Exception
reporting.
However, when I use orElseThrow
of the group inside a lambda I get the following error from the IDE:
Unhandled exception: java.lang.Exception
Follow the code below:
public UserDTO updateUser(UserDTO dto) throws Exception {
if (dto.getId() == null) {
throw new Exception("Usuario não informado corretamente para atualização.");
}
// buscando dados de usuario
final Optional<UserEntity> user = repository.findById(dto.getId());
user.ifPresent(u -> {
u.setEmail(dto.getEmail());
u.setStatus(dto.isStatus());
// buscando referencia de grupo
final Optional<GroupEntity> group = groupService.getGroup(dto.getGroupId());
// grupo nao encontrado
group.orElseThrow(() -> new Exception("Grupo não encontrado"));
// grupo encontrado
group.ifPresent(g -> {
u.setGroupEntity(g);
});
repository.save(u);
});
// usuario nao encontrado
user.orElseThrow(() -> new Exception("Usuario não encontrado"));
return dto;
}