From dae1b7cd38fbdae22cdcf79fb9ea09e2ab640255 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 13 Apr 2022 16:40:10 +0300 Subject: Handle email exceptions --- .../java/com/juick/service/UserServiceImpl.java | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index fd42048c..36f37d5e 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -28,7 +28,10 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.RowMapper; @@ -57,6 +60,8 @@ import java.util.UUID; @Repository public class UserServiceImpl extends BaseJdbcService implements UserService { + private static final Logger logger = LoggerFactory.getLogger("UserService"); + @Value("${juick.admin_users:}") List adminUsers; @@ -173,18 +178,23 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Nonnull public User getUserByEmail(String email) { if (StringUtils.isNotBlank(email)) { - List list = getJdbcTemplate().query( - "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, " + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified " + - "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + - "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + - "LEFT JOIN emails e ON e.user_id = u.id " + - "WHERE u.id = (SELECT DISTINCT user_id FROM emails WHERE email = ?)", - new UserMapper(), - email); - - if (!list.isEmpty()) - return list.get(0); + try { + List list = getJdbcTemplate().query( + "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified " + + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + + "LEFT JOIN emails e ON e.user_id = u.id " + + "WHERE u.id = (SELECT DISTINCT user_id FROM emails WHERE email = ?)", + new UserMapper(), + email); + + if (!list.isEmpty()) + return list.get(0); + } catch (DataIntegrityViolationException e) { + logger.warn("Data exception with email {}", email); + return AnonymousUser.INSTANCE; + } } return AnonymousUser.INSTANCE; } -- cgit v1.2.3