Skip to content

Commit

Permalink
Merge pull request #318 from WE-ARE-RACCOONS/RAC-449
Browse files Browse the repository at this point in the history
RAC-449 feat : 500μ—λŸ¬ νƒμ§€μ‹œ μŠ¬λž™ λ°œμ†‘ μΆ”κ°€
  • Loading branch information
ywj9811 authored Oct 23, 2024
2 parents 88fe559 + 41f2900 commit fd9ca0f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.postgraduate.global.constant.ErrorCode;
import com.postgraduate.global.dto.ErrorResponse;
import com.postgraduate.global.dto.ResponseDto;
import com.postgraduate.global.slack.SlackErrorMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,6 +18,7 @@
@RequiredArgsConstructor
public class GlobalExceptionHandler {
private static final String LOG_FORMAT = "Code : {}, Message : {}";
private final SlackErrorMessage slackErrorMessage;

@ExceptionHandler(ApplicationException.class)
public ResponseEntity<ResponseDto<ErrorResponse>> handleApplicationException(ApplicationException ex) {
Expand All @@ -34,6 +36,7 @@ public ResponseEntity<ResponseDto<ErrorResponse>> handleArgumentValidException(M
public ResponseEntity<ResponseDto<ErrorResponse>> handleInternalServerException(Exception ex) {
log.error(LOG_FORMAT, "500", ex.getStackTrace());
log.error("errorMessage : {}", ex.getMessage());
slackErrorMessage.sendSlackServerError(ex);
return ResponseEntity.internalServerError().build();
}
}
28 changes: 26 additions & 2 deletions src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import static com.postgraduate.global.slack.SlackUtils.generateSlackField;
Expand Down Expand Up @@ -63,6 +64,19 @@ public void sendSlackBizppurioError(String phoneNumber) {
}
}

public void sendSlackServerError(Exception ex) {
try {
slackClient.send(logWebHookUrl, Payload.builder()
.text("μ•Œλ¦Όν†‘ λ°œμ†‘ μ‹€νŒ¨! 확인 μš”λ§!!")
.attachments(
List.of(generateServerErrorSlackAttachMent(ex))
)
.build());
} catch (IOException e) {
log.error("slack 전솑 였λ₯˜");
}
}

private Attachment generateMentoringErrorSlackAttachment(Long mentoringId, Throwable ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -74,7 +88,6 @@ private Attachment generateMentoringErrorSlackAttachment(Long mentoringId, Throw
))
.build();
}

private Attachment generateSalaryErrorSlackAttachment(Long seniorId, Throwable ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -86,7 +99,6 @@ private Attachment generateSalaryErrorSlackAttachment(Long seniorId, Throwable e
))
.build();
}

private Attachment generateBizppurioErrorSlackAttachment(String phoneNumber) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -97,4 +109,16 @@ private Attachment generateBizppurioErrorSlackAttachment(String phoneNumber) {
))
.build();
}

private Attachment generateServerErrorSlackAttachMent(Exception ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
.color("ff0000")
.title(requestTime + "에 λ°œμƒν•œ 500 μ—λŸ¬")
.fields(List.of(
generateSlackField("500 μ—λŸ¬ λ°œμƒ", ex.getMessage()),
generateSlackField("StackTrace", Arrays.toString(ex.getStackTrace()))
))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,6 @@ void updateInfo() throws Exception {
.andExpect(jsonPath("$.message").value(UPDATE_USER_INFO.getMessage()));
}

@Test
@DisplayName("잘λͺ»λœ 번호둜 μˆ˜μ •ν•  수 μ—†λ‹€")
@WithMockUser
void updateInvalidPhoneNumber() throws Exception {
UserInfoRequest userInfoRequest = new UserInfoRequest("new_profile", "newν›„λ°°", "phoneNumber");
String request = objectMapper.writeValueAsString(
userInfoRequest
);

willThrow(new PhoneNumberException())
.given(userManageUseCase)
.updateInfo(any(), any());

mvc.perform(patch("/user/me/info")
.with(csrf())
.header(HttpHeaders.AUTHORIZATION, BEARER )
.content(request)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(UserResponseCode.INVALID_PHONE_NUMBER.getCode()))
.andExpect(jsonPath("$.message").value(UserResponseMessage.INVALID_PHONE_NUMBER.getMessage()));
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
@WithMockUser
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/postgraduate/support/ControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.postgraduate.domain.member.user.application.usecase.UserMyPageUseCase;
import com.postgraduate.domain.member.user.presentation.UserController;
import com.postgraduate.global.aop.lock.DistributeLockAspect;
import com.postgraduate.global.exception.GlobalExceptionHandler;
import com.postgraduate.global.slack.SlackLogErrorMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class ControllerTest {
protected SlackLogErrorMessage slackLogErrorMessage;
@MockBean
protected DistributeLockAspect distributeLockAspect;
@MockBean
protected GlobalExceptionHandler globalExceptionHandler;
protected Resource resource = new Resource();

}

0 comments on commit fd9ca0f

Please sign in to comment.