|
9 | 9 | import static org.springframework.http.HttpStatus.NOT_FOUND;
|
10 | 10 | import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
11 | 11 |
|
| 12 | +import com.yello.server.domain.authorization.JwtTokenProvider; |
12 | 13 | import com.yello.server.domain.authorization.exception.AuthBadRequestException;
|
13 | 14 | import com.yello.server.domain.authorization.exception.CustomAuthenticationException;
|
14 | 15 | import com.yello.server.domain.authorization.exception.ExpiredTokenException;
|
|
21 | 22 | import com.yello.server.domain.friend.exception.FriendNotFoundException;
|
22 | 23 | import com.yello.server.domain.group.exception.GroupNotFoundException;
|
23 | 24 | import com.yello.server.domain.question.exception.QuestionNotFoundException;
|
| 25 | +import com.yello.server.domain.user.entity.User; |
24 | 26 | import com.yello.server.domain.user.exception.UserBadRequestException;
|
25 | 27 | import com.yello.server.domain.user.exception.UserConflictException;
|
26 | 28 | import com.yello.server.domain.user.exception.UserException;
|
27 | 29 | import com.yello.server.domain.user.exception.UserNotFoundException;
|
| 30 | +import com.yello.server.domain.user.repository.UserRepository; |
28 | 31 | import com.yello.server.domain.vote.exception.VoteForbiddenException;
|
29 | 32 | import com.yello.server.domain.vote.exception.VoteNotFoundException;
|
30 | 33 | import com.yello.server.global.common.dto.BaseResponse;
|
31 | 34 | import com.yello.server.infrastructure.redis.exception.RedisException;
|
32 | 35 | import com.yello.server.infrastructure.redis.exception.RedisNotFoundException;
|
| 36 | +import java.time.LocalDateTime; |
| 37 | +import java.time.format.DateTimeFormatter; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Optional; |
| 43 | +import javax.servlet.http.HttpServletRequest; |
| 44 | +import lombok.RequiredArgsConstructor; |
| 45 | +import net.gpedro.integrations.slack.SlackApi; |
| 46 | +import net.gpedro.integrations.slack.SlackAttachment; |
| 47 | +import net.gpedro.integrations.slack.SlackField; |
| 48 | +import net.gpedro.integrations.slack.SlackMessage; |
| 49 | +import org.springframework.core.task.TaskExecutor; |
| 50 | +import org.springframework.http.HttpHeaders; |
33 | 51 | import org.springframework.http.ResponseEntity;
|
34 | 52 | import org.springframework.http.converter.HttpMessageConversionException;
|
35 | 53 | import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
40 | 58 | import org.springframework.web.bind.annotation.RestControllerAdvice;
|
41 | 59 |
|
42 | 60 | @RestControllerAdvice
|
| 61 | +@RequiredArgsConstructor |
43 | 62 | public class ControllerExceptionAdvice {
|
44 | 63 |
|
| 64 | + private final TaskExecutor taskExecutor; |
| 65 | + private final SlackApi slackApi; |
| 66 | + private final UserRepository userRepository; |
| 67 | + private final JwtTokenProvider jwtTokenProvider; |
| 68 | + |
| 69 | + @ExceptionHandler(Exception.class) |
| 70 | + void handleException(HttpServletRequest request, Exception exception) throws Exception { |
| 71 | + SlackAttachment slackAttachment = new SlackAttachment(); |
| 72 | + |
| 73 | + slackAttachment.setFallback("Error"); |
| 74 | + slackAttachment.setColor("danger"); |
| 75 | + slackAttachment.setTitle("긴급 환자가 이송되었습니다"); |
| 76 | + slackAttachment.setTitleLink(request.getContextPath()); |
| 77 | + slackAttachment.setText(Arrays.toString(exception.getStackTrace())); |
| 78 | + slackAttachment.setColor("danger"); |
| 79 | + |
| 80 | + List<SlackField> slackFieldList = new ArrayList<>(); |
| 81 | + slackFieldList.add(new SlackField().setTitle("Request URL").setValue(request.getRequestURL().toString())); |
| 82 | + slackFieldList.add(new SlackField().setTitle("Request Method").setValue(request.getMethod())); |
| 83 | + slackFieldList.add(new SlackField().setTitle("Request Time").setValue( |
| 84 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()))); |
| 85 | + slackFieldList.add(new SlackField().setTitle("Request IP").setValue(request.getRemoteAddr())); |
| 86 | + slackFieldList.add( |
| 87 | + new SlackField().setTitle("Request User-Agent").setValue(request.getHeader(HttpHeaders.USER_AGENT))); |
| 88 | + slackFieldList.add( |
| 89 | + new SlackField().setTitle("인증/인가 정보 - Authorization") |
| 90 | + .setValue(request.getHeader(HttpHeaders.AUTHORIZATION))); |
| 91 | + |
| 92 | + final String token = request.getHeader(HttpHeaders.AUTHORIZATION).substring("Bearer ".length()); |
| 93 | + final Long userId = jwtTokenProvider.getUserId(token); |
| 94 | + final Optional<User> user = userRepository.findById(userId); |
| 95 | + String userInfo = ""; |
| 96 | + userInfo = user.map(value -> "userId : " + userId |
| 97 | + + "\nyelloId : " + value.getYelloId() |
| 98 | + + "\ndeviceToken : " + value.getDeviceToken()).orElseGet(() -> "userId : " + userId); |
| 99 | + slackFieldList.add( |
| 100 | + new SlackField().setTitle("인증/인가 정보 - 유저").setValue(userInfo)); |
| 101 | + |
| 102 | + slackAttachment.setFields(slackFieldList); |
| 103 | + |
| 104 | + SlackMessage slackMessage = new SlackMessage(); |
| 105 | + slackMessage.setAttachments(Collections.singletonList(slackAttachment)); |
| 106 | + slackMessage.setText("긴급 환자가 이송되었습니다"); |
| 107 | + slackMessage.setUsername("옐로 소방서"); |
| 108 | + |
| 109 | + Runnable runnable = () -> slackApi.call(slackMessage); |
| 110 | + taskExecutor.execute(runnable); |
| 111 | + throw exception; |
| 112 | + } |
| 113 | + |
45 | 114 | /**
|
46 | 115 | * 400 BAD REQUEST
|
47 | 116 | */
|
|
0 commit comments